Random Numbers
-
i want to generate some random numbers, but with in a limit ( i.e i want to generate the random numbers with in 50) how can i do it by using the rand() ? Thanks in advance Ninety-eight percent of the thrill comes from knowing that the thing you designed works, and works almost the way you expected it would. If that happens, part of you is in that machine.
-
i want to generate some random numbers, but with in a limit ( i.e i want to generate the random numbers with in 50) how can i do it by using the rand() ? Thanks in advance Ninety-eight percent of the thrill comes from knowing that the thing you designed works, and works almost the way you expected it would. If that happens, part of you is in that machine.
Try
// Seed the random-number generator with current time so that // the numbers will be different every time we run. srand( (unsigned)time( NULL ) ); // include for this for( int i = 0; i < 20; i++ ) { int n = int (50 \* ( (double) rand() / RAND\_MAX )); cout << n << "\\n"; }
Typical output: 4 48 10 16 27 12 10 27 45 39 24 15 7 3 12 22 24 25 19 28 Kevin
-
i want to generate some random numbers, but with in a limit ( i.e i want to generate the random numbers with in 50) how can i do it by using the rand() ? Thanks in advance Ninety-eight percent of the thrill comes from knowing that the thing you designed works, and works almost the way you expected it would. If that happens, part of you is in that machine.
Hi, try this: srand( (unsigned)time( NULL )); int num = rand()%50; Regards, Eli
-
Hi, try this: srand( (unsigned)time( NULL )); int num = rand()%50; Regards, Eli
yeah great...thankyou Ninety-eight percent of the thrill comes from knowing that the thing you designed works, and works almost the way you expected it would. If that happens, part of you is in that machine.
-
Hi, try this: srand( (unsigned)time( NULL )); int num = rand()%50; Regards, Eli
Of course! Much simpler than mine! Kevin
-
i want to generate some random numbers, but with in a limit ( i.e i want to generate the random numbers with in 50) how can i do it by using the rand() ? Thanks in advance Ninety-eight percent of the thrill comes from knowing that the thing you designed works, and works almost the way you expected it would. If that happens, part of you is in that machine.
-
Try
// Seed the random-number generator with current time so that // the numbers will be different every time we run. srand( (unsigned)time( NULL ) ); // include for this for( int i = 0; i < 20; i++ ) { int n = int (50 \* ( (double) rand() / RAND\_MAX )); cout << n << "\\n"; }
Typical output: 4 48 10 16 27 12 10 27 45 39 24 15 7 3 12 22 24 25 19 28 Kevin
Kevin McFarlane wrote: int n = int (50 * ( (double) rand() / RAND_MAX )); Aaaah, finally someone else who knows the best way to use
rand()
. Most people just use the%
operator, but statistically, using your approach leads to more randomness, as therand()
algorithm has the property that the higher-order bits are more random than the lower order bits. Or did you just guess? ;) Either way, well done :)Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
-
i want to generate some random numbers, but with in a limit ( i.e i want to generate the random numbers with in 50) how can i do it by using the rand() ? Thanks in advance Ninety-eight percent of the thrill comes from knowing that the thing you designed works, and works almost the way you expected it would. If that happens, part of you is in that machine.
renjith_sree wrote: i want to generate some random numbers, Unfortunately this is impossible using a computer algorithm. The best you can hope for is a pseudo-random number. renjith_sree wrote: but with in a limit ( i.e i want to generate the random numbers with in 50)... Is 50 the upper limit or the lower limit?
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
-
Kevin McFarlane wrote: int n = int (50 * ( (double) rand() / RAND_MAX )); Aaaah, finally someone else who knows the best way to use
rand()
. Most people just use the%
operator, but statistically, using your approach leads to more randomness, as therand()
algorithm has the property that the higher-order bits are more random than the lower order bits. Or did you just guess? ;) Either way, well done :)Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
I just guessed. In fact I'd thought that eli15021979's % method was slicker! But you learn something new every day.:) Kevin
-
I just guessed. In fact I'd thought that eli15021979's % method was slicker! But you learn something new every day.:) Kevin
Kevin McFarlane wrote: In fact I'd thought that eli15021979's % method was slicker! It is :) And it will be good enough most of the time, but technically you can do better without having to change an awful lot ;). In fact, if you use the method you originally posted, modifying it to work with floating-point numbers is relatively trivial - a lot easier than converting one using the modulo operator, since the modulo operator doesn't work for floating-point numbers, well not in C++ at least...
Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"