|
-
January 22nd, 2003, 10:25 PM
#1
Print a random number from set
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?
-
January 22nd, 2003, 10:59 PM
#2
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:
Code:
cout << rand() * whatever << endl;
--Paul
-
January 22nd, 2003, 11:03 PM
#3
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() )
Last edited by Philip Nicoletti; January 23rd, 2003 at 12:33 AM.
-
January 22nd, 2003, 11:24 PM
#4
int numbers[] = { 6,10,14,18,22 };
see what you can do
-
January 22nd, 2003, 11:41 PM
#5
(^_^)
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,
-
January 23rd, 2003, 12:31 AM
#6
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).
-
January 23rd, 2003, 01:53 AM
#7
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.
-
January 23rd, 2003, 06:26 AM
#8
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.
Last edited by CBasicNet; January 23rd, 2003 at 06:50 AM.
-
January 23rd, 2003, 07:23 AM
#9
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
-
January 23rd, 2003, 09:58 AM
#10
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;
-
January 23rd, 2003, 11:17 AM
#11
I never noticed that I would have done;
int numbers[] = { 6,10,14,18,22 };
cout << numbers[ int(rand() % 5) ];
-
January 23rd, 2003, 11:34 AM
#12
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/Nu.../bookcpdf.html
Last edited by KevinHall; January 23rd, 2003 at 12:21 PM.
-
January 23rd, 2003, 12:02 PM
#13
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 ?
-
January 23rd, 2003, 12:15 PM
#14
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.
-
January 23rd, 2003, 12:19 PM
#15
CBasicNet,
You're right -- brain fart there. For some reason I was thinking 32767 was 2^16 - 1 not 2^15 -1.
- Kevin
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|