Help Wanted

randon number generator – en972

en972

Member

Posts: 562
From: NOT TELLING!
Registered: 08-27-2004
I'm working on a random number generator

what is the best way to generate random numbers

------------------
Keep the holy day sacred.......halllllllukan

CobraA1

Member

Posts: 926
From: MN
Registered: 02-19-2001
Use rand(). Don't forget to seed it.

------------------
"The very idea of freedom presupposes some objective moral law which overarches rulers and ruled alike." -- C. S. Lewis (1898 - 1963), "The Poison of Subjectivism" (from Christian Reflections; p. 108)

Switch Mayhem now available! Get it here
Codename: Roler - hoping to get more done over the holidays . . .

CapnStank

Member

Posts: 214
From: Sask, Canada
Registered: 12-16-2004
What code? If it's C i can hook you up with a header file that makes it simple, just add:

randomize();
<variable> = random(#);

But it'll cost you

------------------
The Stank pwns you!

[This message has been edited by CapnStank (edited January 30, 2005).]

en972

Member

Posts: 562
From: NOT TELLING!
Registered: 08-27-2004
oops, forgot to say I was using C++.

thanks cobra...I know what you mean by seed, but how do i do it

------------------
Keep the holy day sacred.......halllllllukan

HeardTheWord

Member

Posts: 224
From: Des Moines, IA
Registered: 08-16-2004
Alright here is a lengthy explanation. You could just call rand() to get a random number. The problem is if you don't seed the random generator you will get the same sequence of numbers everytime you run the program. That is where srand() comes into the program. Here is an example of srand().
srand( (unsigned) time(0) );

You might notice (unsigned) is before time(0). At this point it would be wise to ignore it (this just prevents a warning). The time() function is out of the time.h library. We pass it a zero just to get the current time(I believe that is correct). After the call to srand() we can use rand() to get a truly random number.
int number = rand();

You could simply use rand() by itself if you didn't care what the number was. If you want a range of numbers though this won't work. We need to use the modulus symbol to set this range. What modulus does is divide by a certain number and gives back the remainder. If you want the range to truly be between 1-range then you must add 1. Here is a better example of the previous code.
int number = rand() % 100 + 1;

I hope this makes random numbers a bit less confusing. Try to use this with your age program and create a random generated age. Remember the rand() function returns an int so you should use variables that fit that mold.

Good luck and God bless!

en972

Member

Posts: 562
From: NOT TELLING!
Registered: 08-27-2004
dude, you did it again. Thanks alot.

two question, why would i want it to generate a random age? would I do that so the program could run itself?

and how do i make it sort?

have a good day in Christ

------------------
Keep the holy day sacred.......halllllllukan

[This message has been edited by en972 (edited February 01, 2005).]

mellonamin

Member

Posts: 119
From: Maryville, TN, United States
Registered: 11-16-2004
Hey, how can I create more than one random number. I need to create four different random numbers and I don't know how to do that....

------------------
Vita sine Ieso est mors.
Life without Jesus is death.
Enter The Circle

HeardTheWord

Member

Posts: 224
From: Des Moines, IA
Registered: 08-16-2004
@en972

Well you wouldn't have to add a random number to your age program. You should play around with it a bit so you understand how it works though.
Your other question is a bit more complicated. There are algorithms that can sort numbers. Search for bubble sort or quick sort and you should find a good sorting algorithm.

@mellonamin

If you want to create more than one random number, all you have to do is create more variables.
Here is an example:

void main() {
// Create our integer variables
int randnum1, randnum2;

// This number is completely random
randnum1 = rand();

// This number ranges from 1-50
randnum2 = rand()%50+1;

// Print out numbers
cout << "First Number: " << randnum1 << endl;
cout << "Second Number: " << randnum2 << endl;

// Just used to hold everything on the screen
cin >> randnum1;
}

[This message has been edited by HeardTheWord (edited February 01, 2005).]

mellonamin

Member

Posts: 119
From: Maryville, TN, United States
Registered: 11-16-2004
I am making a code breaker type game and I need to create 4 totally different random numbers, any time I do what you just did, it returns the exact same 4 numbers to each variable...even if I seed it

------------------
Vita sine Ieso est mors.
Life without Jesus is death.
Enter The Circle

HeardTheWord

Member

Posts: 224
From: Des Moines, IA
Registered: 08-16-2004
Remove:
#include <time.h>

I am not sure why the C header files do not work. I was able to get separate random numbers this way.

[This message has been edited by HeardTheWord (edited February 01, 2005).]

mellonamin

Member

Posts: 119
From: Maryville, TN, United States
Registered: 11-16-2004
I am using #include <ctime>...
That is the only way I could figure out how to seed it
------------------
Vita sine Ieso est mors.
Life without Jesus is death.
Enter The Circle

[This message has been edited by mellonamin (edited February 02, 2005).]

Seven7
Member

Posts: 50
From: USA
Registered: 03-16-2005

FYI:

Unless your computer comes with a radioactive decay of Cesium to
generate your random numbers, the "rand()" function only generates
pseudo-numbers. The "rand()" function only appears random, achieving a
relatively flat distribution curve over a generation of billions of
numbers (seed), the sequence will be exactly the same. A true random
number generator will never repeat like this. A true random number
generator will always provide a good distribution curve (neither skewed
or "near flat").

Here is an example of why you would not want to use "rand()" and why you
should always create your own random number generator;
Lets say that you have a AI character that taunts. If you use the "rand()"
implementation, you will get a "true" character behavior. On the other hand
if you use an implementation other than the "rand()", you get an entirely
different pattern in AI behavior. This means that your character will
display "unpredictable" behavior. This is because your random number
generator is drawing from a good or "true" (better) distribution of
numbers. By using the "rand()" function you would not be drawing from a
good set of random numbers. (Have you ever noticed how characters in a
single player game react?) Drawing from a good distribution of numbers
is what you want to do when developing AI characters.

In short what game developers do is create a set of random numbers and a
set of objects (i.e. AI characters) then map each set of random numbers to
each object thus producing each and ever AI object a unique set of
behaviors.


Food for thought.


Briant

Member

Posts: 742
From: Stony Plain, Alberta, Canada
Registered: 01-20-2001
Hi Seven7,

Welcome to the board!

The problem you describe is true - however, it is easily avoided by seeding the generator with something that will be different each time, such as the current time (as in the example given above by HeardTheWord), the number of milliseconds the computer has been on, a hash of all the keystrokes and mouse movements since the program started, etc.

------------------
Brian

CobraA1

Member

Posts: 926
From: MN
Registered: 02-19-2001
If seeded properly, most pseudo-random number generators are fine for most purposes.

------------------
"The very idea of freedom presupposes some objective moral law which overarches rulers and ruled alike." -- C. S. Lewis (1898 - 1963), "The Poison of Subjectivism" (from Christian Reflections; p. 108)

Switch Mayhem now available! Get it here
Codename: Roler - hoping to get more done over the holidays . . .

ArchAngel

Member

Posts: 3450
From: SV, CA, USA
Registered: 01-29-2002
well, there is not real random number generator. lol.

um, lemme see... in java, the
random() command will give you a random double number between 0 and 1. then, u just modify it to get whatever range you want.

so...
int rand = (int) (random() * 10 + 1);

should get you a random integer between 1 and 10. want twenty, change the 10 into twenty? want between 10 and 20? change the 1 into a 10. stuff like that.

no need to seed here, cuz it seeds from the clock. pretty cool if you ask me...

------------------
Soterion Studios

Seven7
Member

Posts: 50
From: USA
Registered: 03-16-2005

I think the bigger picture is lost here, which was my point if the first
place. I most certainly agree for simple projects using the "rand()"
(or other variant functions) you will have no problems. I am not specifying
a random number generator on a specific platform, but rather looking at
"rand()" from the mathematical point of view. Whether you use C, C++,
Java, it will not make a difference. Also, whether you seed a function
via some time routine, you end up with the same exact problem. Its not a
subject of debate, its a subject of architecture and design based on
mathematics. The "rand()" function still pools from a relatively flat
distribution. This IS the bigger picture and mathematical fact, and there
is no way of getting a “fair” distribution of numbers from a pool without
some way of measuring radioactive decay. Java, C, C++ or some other
language has absolutely no baring on how numbers are generated, the
underling techniques and design are the same.

I am certainly not arguing that people should not use the “rand()”
function. My first post was an “FYI” not to be meant to argue the finer
points on a language, but rather to enlighten on the mathematical points
of the “rand()” function.

Given the following code;

int rand = (int) (random() * 10 + 1);

If a person is happy about the results not being pooled from a fair
distribution of numbers then use it. However, you would not want to do
this with, again, in large scale gamming engines. CobraA1 is hit the nail
on the head. If you don’t care about the finer points of randomness,
then just use “rand()”. The implentation of code for some random number
generator, i.e. "rand()" is based on it mathematics not the code itself.
The code in its self is pointless, its only the math behind the code
that matters the most. When I first learned that I could use "rand()" or
some other variation of it, the first thing I asked myself is "how do
they generate those numbers?", "How is the code REALLY implemented,(how
did they write the "rand()" function)?" When I learned that the "rand()"
finction pools from numbers on the physical architecture, I knew right
away there were problems. So ArchAngel hit on a good topic from
mathematics.

What will be really shock to most people is that there is no way of
pooling a TRULY random number! Think about it, there are infinite set of
numbers right? How do you give an equal amount of chance of pooling one
number from an set of infinite numbers? It is impossible!

CoolJ

Member

Posts: 354
From: ny
Registered: 07-11-2004
wow..I never knew...I guess you can pull realtime random #s from the following sites..

random based on atmospheric noise.. BETTER
http://www.random.org

random based on radioactive decay... BEST
http://www.fourmilab.ch/hotbits/

Briant

Member

Posts: 742
From: Stony Plain, Alberta, Canada
Registered: 01-20-2001
Hey Seven7,

I realize and understand those limitations of rand(). However, the even bigger picture is "how much does it matter?" You said about rand(), "However, you would not want to do this with, again, in large scale gamming engines."

Really, why not? I cannot think of any examples in a gaming engine where it would matter. (I'm not arguing, just wondering. )

------------------
Brian

Seven7
Member

Posts: 50
From: USA
Registered: 03-16-2005
Hi BrianT,

When developing large scale physics engines, i.e. games, developers
generally like to stay away from using built in functions. (This not
only applies to functions like “rand()”, but also to “new” and “delete”
as well.) Of course this means they need to develop there own
functions like "rand()". Developers want to be able to develop
software that is more “mathematically sound”. They want to make
functions that provide “best opportunity” of getting some random
number and the “rand()” function does not provide the “best” fit
for physics engines. So by providing there own random number
generator, they can get “better” or “truer” random numbers.
It all boils down to statistics.

The “rand()” function polls numbers from a pool of relative flat
distribution curve, there just no arguing that, it’s the way it was
designed. Now then, if we can write some code that will improve that
distribution curve ,we can use it in our physics engine (i.e. game )
and get better results, for example better random interaction with
objects.

For your readers learning how to use the “rand()” function; use it and
don’t worry about the mathematical theory behind random numbers.


Jim

Klumsy

Administrator

Posts: 1061
From: Port Angeles, WA, USA
Registered: 10-25-2001
actually game developers want something that does the job well, but fast, if they wanted the best, they would never use polygon based models but would be raytracing everything, and all popular game alogirthms out there take alot of mathmatical liberties to get them faster. so especialyl with a random number generator a game developer wants something that works pretty much like rand (good enough), but that is as fast as possible.

------------------
Karl /GODCENTRIC
Visionary Media
the creative submitted to the divine.
Husband of my amazing wife Aleshia
Klumsy@xtra.co.nz

goop2

Member

Posts: 1059
From:
Registered: 06-30-2004
Hmmm... I just noticed, thats a dirtsurfer in your avatar...

------------------
------------------------

I dont like siggys. They are to hard to think up :(

en972

Member

Posts: 562
From: NOT TELLING!
Registered: 08-27-2004
LOL! That was sooo random (pun not intended)

------------------
Keep the holy day sacred.......halllllllukan