Click to See Complete Forum and Search --> : Print a random number from set


HHughes
January 22nd, 2003, 09:25 PM
For each of the following sets of integers, write a single
statement that will print a number at random from the set.

c) 6,10,14,18,22

I can't do this in one line. the best i can do is:

num = 1;
while((num % 4) || (num > 24)) num = rand() % 24 + 8;
num -= 2;
cout << "Number in set {6,10,14,18,22}: " << num << endl;
return 0;

Any thoughts?

PaulWendt
January 22nd, 2003, 09:59 PM
I'm not checking your actual code at all, but it seems like the
question's only asking you to print out one number so there
doesn't appear to be a need for a loop. You can probably just
do something like:

cout << rand() * whatever << endl;


--Paul

Philip Nicoletti
January 22nd, 2003, 10:03 PM
Well, since this looks like a homework problem,
I'll just give a hint :

You want to print out one of five numbers. rand()%5
will generate one of five numbers (0,1,2,3,4)

So (the simplest) thing to do is to come up with an
expression such that:

when rand() returns 0, the expression evaluates to 6,
when rand() returns 1, the expression evaluates to 10,
when rand() returns 2, the expression evaluates to 14,

etc.

As Paul mentioned, there is no need for a loop.

Also, I am not sure if rand() returns a non-negative
number on all systems, so maybe you might need to use

static_cast<unsigned int>( rand() )

mwilliamson
January 22nd, 2003, 10:24 PM
int numbers[] = { 6,10,14,18,22 };

see what you can do :)

Wfranc
January 22nd, 2003, 10:41 PM
I think you can also do like this (hint): enter stepnumber to jump(in your ex, it is 4), then input lowerlimit(6) and upperlimit(22), then use random funtion, and finally output the number after randomly chosen.

Hope this helps.



Regardlessly,

KevinHall
January 22nd, 2003, 11:31 PM
HHughes, this is a very simple program so it doesn't really matter here, but to generate random integers from 0 to a certain maximum, the follwoing doesn't produce very good random results:

x = rand() % (max+1)

Instead the following statement produces better results:

x = ((unsigned long)((rand()/(RAND_MAX+1.0))*(max+1.0)))

The long in the cast above could be replaced by any unsigned integer type.

Anyway, you'd have to perform an analysis on many random numbers before you really noticed the difference, but I thought I'd let you know in case someday you come across a project where generating sequences of random numbers was important (of coarse you probably wouldn't want to use rand() then anyway -- oh well).

filthy_mcnasty
January 23rd, 2003, 12:53 AM
i suggest Philip Nicoletti's suggestion.

i've never seen rand() generate a negative number. it's an int so i guess it could support it but i am next to certain that it doesn't unless you were to change it around somehow. modulus division operator is the same.

CBasicNet
January 23rd, 2003, 05:26 AM
Hi Kevin,

I'm no random number generator expert but I saw someone posted this statement in GameDev forum.

x=max*rand()/(RAND_MAX+1);//generate random numbers from 0 to max-1

And according to that poster, this will evaluate to this(below), in an optimising compiler,

x=( max*rand() )>>15;

Which he claimed is faster than using modulus. And note he did not have any int or float conversions.

Although I have never tried it out myself, I feel that what he said, made sense to me.

For those who might not be familiar, RAND_MAX is 32767(0x7fff) in VC CRT library.

CBasicNet
January 23rd, 2003, 06:23 AM
In case anyone wonder why num/32768 EQUALS num>>15,

num/2 == num>>1
num/4 == num>>2
num/8 == num>>3
num/16 == num>>4
num/32 == num>>5
.
.
.
.
num/16384 == num>>14
num/32768 == num>>15

sagmam
January 23rd, 2003, 08:58 AM
very simple:

you have 5 numbers, with an indentical difference of 4 between them.

so you need the following expression:
cout << 6 + (rand() % 5) * 4;

mwilliamson
January 23rd, 2003, 10:17 AM
I never noticed that :) I would have done;
int numbers[] = { 6,10,14,18,22 };
cout << numbers[ int(rand() % 5) ];

KevinHall
January 23rd, 2003, 10:34 AM
Hi Kevin,

I'm no random number generator expert but I saw someone posted this statement in GameDev forum.

x=max*rand()/(RAND_MAX+1);//generate random numbers from 0 to max-1

And according to that poster, this will evaluate to this(below), in an optimising compiler,

x=( max*rand() )>>15;

Which he claimed is faster than using modulus. And note he did not have any int or float conversions.

Although I have never tried it out myself, I feel that what he said, made sense to me.

For those who might not be familiar, RAND_MAX is 32767(0x7fff) in VC CRT library.

CBasicNet,

You bring up a good point :) however:

You are only right assuming:
* RAND_MAX is 32767 (this is not specified by ANSI)
Incorrect: * max <= RAND_MAX/2 (Thanks CBasicNet)
* and the size of int is 32 bits (again not specified by ANSI)

So for VC6, this would work, but for LabWindows CVI or old 16-bit compilers (or new compilers where RAND_MAX was 2147483647) this would not work. The method I pointed out will be portable across different compilers.

For anyone who is interested, an excellent reference on pseudo-random number generation should check out Numerical recipes' chapter 7 (especially section 7.1): http://www.ulib.org/webRoot/Books/Numerical_Recipes/bookcpdf.html

CBasicNet
January 23rd, 2003, 11:02 AM
Originally posted by KevinHall
You are only right assuming:
* RAND_MAX is 32767 (this is not specified by ANSI)
* max <= RAND_MAX/2
* and the size of int is 32 bits (again not specified by ANSI)


if RAND_MAX is 32767 , why max should be <= RAND_MAX/2 when RAND_MAX * RAND_MAX is still < 2147483647 ?

PaulWendt
January 23rd, 2003, 11:15 AM
Originally posted by mwilliamson
I never noticed that :) I would have done;
int numbers[] = { 6,10,14,18,22 };
cout << numbers[ int(rand() % 5) ];

That's two lines, though.

KevinHall
January 23rd, 2003, 11:19 AM
CBasicNet,

You're right -- brain fart there. For some reason I was thinking 32767 was 2^16 - 1 not 2^15 -1.

- Kevin

sagmam
January 27th, 2003, 08:52 AM
But I gave you a way to do it in a single line.. more important, in a single statement:

cout << 6 + (rand() % 5) * 4;

mwilliamson
January 27th, 2003, 09:13 AM
Originally posted by PaulWendt


That's two lines, though.

:) but my code will work for any 5 numbers.

PaulWendt
January 27th, 2003, 09:19 AM
Originally posted by mwilliamson


:) but my code will work for any 5 numbers.

Perhaps ... but it's beyond the realm of the OP's question.
It's quite irrelevant anyway, I suppose, since it was only an
academic question.