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 ------------------ |
|||
CobraA1![]() Member Posts: 926 From: MN Registered: 02-19-2001 |
Use rand(). Don't forget to seed it. ------------------ Switch Mayhem now available! Get it here |
|||
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(); But it'll cost 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 ------------------ |
|||
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().
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.
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.
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 ------------------ [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.... ------------------ |
|||
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. @mellonamin If you want to create more than one random number, all you have to do is create more variables.
[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 ------------------ |
|||
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 Here is an example of why you would not want to use "rand()" and why you In short what game developers do is create a set of random numbers and a
|
|||
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. |
|||
CobraA1![]() Member Posts: 926 From: MN Registered: 02-19-2001 |
If seeded properly, most pseudo-random number generators are fine for most purposes. ------------------ Switch Mayhem now available! Get it here |
|||
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 so... 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... ------------------ |
|||
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()” Given the following code; int rand = (int) (random() * 10 + 1); If a person is happy about the results not being pooled from a fair What will be really shock to most people is that there is no way of |
|||
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 random based on radioactive decay... BEST |
|||
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?" Really, why not? I cannot think of any examples in a gaming engine where it would matter. (I'm not arguing, just wondering. |
|||
Seven7 Member Posts: 50 From: USA Registered: 03-16-2005 |
Hi BrianT, When developing large scale physics engines, i.e. games, developers The “rand()” function polls numbers from a pool of relative flat For your readers learning how to use the “rand()” function; use it and
|
|||
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. ------------------ |
|||
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) ------------------ |