#### Archived

This topic is now archived and is closed to further replies.

# Random Numbers

This topic is 5171 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

What is the C standard way of generating a random number between x and y? Could somebody give me an example function? I''ve tried using rand() and srand() but I got predictable results

##### Share on other sites
are you seeding the generator properly? You know that any pseudo-random number generator will return the same numbers when given the same seed right?

(edit: forgot a word in there)

[edited by - andy_fish on October 16, 2003 10:39:04 PM]

##### Share on other sites
to always have random numbers you need to set random with time

#include <time.h>int number;int main(){  // do this a the begining of your program  srand(time(NULL));  for(int i=0; i<10; i++)  {    // this will give you a random number between 10 and 100    number = (rand() % 100) + 10;    printf("%d\n", number);  }    return 0;}

Matt

##### Share on other sites
quote:
Original post by 31337
What is the C standard way of generating a random number between x and y? Could somebody give me an example function? I've tried using rand() and srand() but I got predictable results

Here is better psuedo random number generator, it called Mersenne Twister

As for generating numbers that fall in certain range, you can use the modulous operator(%). For example if you want number between 5 and 20.

#include<iostream>#include<cstdlib>#include<time.h>using namespace std;int main(){  int a = 0;  srand(time(NULL));    a = (rand() % 16) + 5;    return 0;}

Sorry the example is C++ but you should still beable to get the jist.

Anyway hope this helps

[edited by - prh99 on October 16, 2003 10:48:34 PM]

##### Share on other sites
You will always have predictable results. In our universe there is no source of true randomness... well, unless you believe in the platonic-like spirit with free will. anyway, whether you agree with that, all the math your computer can do is predictable so the best thing you can do is make your equation ridiculously complicated taking input from as many things as you can that wont be the same all the time, like your a timer from when the program executed, the system time, number of keystrokes in the last hour, etc. whatever you can come up with. the weirder the better, i think. factor those things into your random calculation, maybe put them into an equation the produces a seed value or something.

##### Share on other sites
There are many real random number generators available for computers. Not that I''m suggesting PC''s normally have such things, but they do exist. Some of the more interesting based on the timing of radioactive decay events.

##### Share on other sites
Yes I''ve heard all of this random number theory, mainly that there is no such thing as a random number but that is only according to the philosophical approach. Mathematically I would say that you can claim numbers generated by observing sound card static impacted by the time, keystrokes, etc... could be considered random.

Thanks for the information guys. Here''s the problem though, I need to generate a lot of numbers all at the same time. What I was doing was:

for(int i=0; i<1000; i++) {srand(GetTickCount());int rand = rand() % 100;}

But what I think was happening was that it was generating them in less than a millisecond or something because I got very bizare results. I''m going to try the time approach.

##### Share on other sites
Thanks a lot guys it worked perfectly, you guys rule.

##### Share on other sites
Hey elite (), sometimes, you don''t want totaly random outputs, to allow you to reproduce bugs, keep networking in sync, and replay game events. That''s what car racing (mostly) on consoles do. They store the seed value, and the initial state of the game, the controler inputs, and from there, they can reproduce exactly the whole state of the race at any given time (although you are only limited to going forward in time). Instead of recording the positions of the cars and other objects throughout the game, It saves a lot of storage space. All the calls to the random number generator will produce the same results again and again when you hit replay. It''s tricky to get it right, but when you do, you feel like a more hip and a more socialy acceptable version of David Blaine. A bit of magic.

##### Share on other sites
quote:
Original post by 31337
for(int i=0; i<1000; i++) {srand(GetTickCount());int rand = rand() % 100;}

But what I think was happening was that it was generating them in less than a millisecond or something because I got very bizare results. I''m going to try the time approach.

This is a very common mistake. srand() *initializes* the pseudo-random number generator, so call it only once. Like this:
srand(GetTickCount());for(int i=0; i<1000; i++) {int rand = rand() % 100;}