Archived

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

31337

Random Numbers

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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;
}



Share this post


Link to post
Share on other sites
I read about a way to generating very high-quality random sequences using a computer and its microphone. In this article, the writer said that since background termic noise is indeed random, then recording it will result in random numbers.
Are they only good pseudorandom, or are they true random numbers?

Share this post


Link to post
Share on other sites