psuedorandom
ok, we all [hopefully] know that computers can only generate psuedo-random numbers, and i am hoping to take advantage of this fact.
i want to use a psuedorandom number generator for the placement of trees, bushes, small stones, et cetera, in my game (tile based). the thing is, when i am createing the maps, i want to see where these "random" shrubs are so i can incorporate them into the map (or give a "lose this hedge" override). so, i need to make sure that the same "random" numbers are generated each time the map is loaded; not only on the same computer, but on all computers on all platforms.
so, does anyone know of the standard c "srand()" & "rand()" will give the same sequence for the same seed on all platforms? i really doubt it, since i think the compiler guys can implement it any way they like as long as they follow certain guidelines. plus, i am not sure how different processors would handle the calculations involved. it''d be sweet if someone said "yes, everything will be just fine," but i''m not holding my breath.
so, does anyone know of an algorithm that will generate psuedorandom numbers based on a seed, that will always give the same sequence regardless of the platform ?
--- krez (krezisback@aol.com)
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
I know a programmer who works for Abbot (a pharmacuetical company). Who worked the programming for the Avionics of the Apache helicopter for Mcdonnal Douglas and (you know what else he did, he programmed the matrix end of the program that Lucasfilm used to do the special effects with on those fighter scenes in Star Wars, pretty sharp guy, knows way too much. I'll pass that one by him. I'll See what he says.
Edited by - Agent on January 3, 2002 12:05:53 AM
Edited by - Agent on January 3, 2002 12:05:53 AM
It puts the Lotion on!
I think most random number generators just do something like this:
Sorry if that code is wrong syntax, the idea is there... C isn''t my native tongue![](smile.gif)
Actually, there was a really good article about this at Gamasutra which was about doing just what you are doing. I can''t remember the name of it, but it was like a 3 part series... Rendering Planetary Bodies was one of the parts, with the first part being what you want I think.
Also, try just looking at the ASM of rand, and you will see how they do theirs. Or go to a website with java-scripts and find one of their random number generators.
Trying is the first step towards failure.
const int primenumber1 = some large prime number;const int primenumber2 = some different prime number;int GetRandom(){ static int Seed; Seed = (Seed + primenumber1) % primenumber2; Return Seed;}
Sorry if that code is wrong syntax, the idea is there... C isn''t my native tongue
![](smile.gif)
Actually, there was a really good article about this at Gamasutra which was about doing just what you are doing. I can''t remember the name of it, but it was like a 3 part series... Rendering Planetary Bodies was one of the parts, with the first part being what you want I think.
Also, try just looking at the ASM of rand, and you will see how they do theirs. Or go to a website with java-scripts and find one of their random number generators.
Trying is the first step towards failure.
Trying is the first step towards failure.
Some algorithms can be found in the numerical recipes:
http://lib-www.lanl.gov/numerical/bookcpdf.html
http://lib-www.lanl.gov/numerical/bookcpdf.html
Don''t trust rand() to give the same sequence from a given seed on different compilers. It seems unlikely that they''ll all be using the same implementation.
If you want a pseudorandom generator that will work the same on any platform, write your own. Find an algorithm to use and code it.
Here''s one that was posted on the flipcode COTD a while back.
If you want a pseudorandom generator that will work the same on any platform, write your own. Find an algorithm to use and code it.
Here''s one that was posted on the flipcode COTD a while back.
Dobbs, that is exactly what i thought.
i thought psuedorandom number generators used floats and whatnot, so i thought even my own algorithm might get different numbers due to the ways processors handle decimals (not my area of expertise, but i''d rather guess and then ask than just hope).
looking through some of these links (thanks by the way yous guys), though, i see that a lot of them use % and no floats, which means as long as the code is the same the output will be the same (right?)... so i just have to pick one and implement my own version of rand() (i shall call it... RanDumb()!!!)
thanks again!
--- krez (krezisback@aol.com)
i thought psuedorandom number generators used floats and whatnot, so i thought even my own algorithm might get different numbers due to the ways processors handle decimals (not my area of expertise, but i''d rather guess and then ask than just hope).
looking through some of these links (thanks by the way yous guys), though, i see that a lot of them use % and no floats, which means as long as the code is the same the output will be the same (right?)... so i just have to pick one and implement my own version of rand() (i shall call it... RanDumb()!!!)
thanks again!
--- krez (krezisback@aol.com)
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
I feel nice today...
I´ll just give way my generator again:
Everything should be easy to undestand, but
seed -= int(seed);
should be explained...It just removes the integer part of the number, which leaves us with a number between 0 - 1
But wont that generate a "stream" which looks something like this:
1 - 5 - 9 - 13 - 17
in other words: wont the seed increase with the same amount all the time???
I´ll just give way my generator again:
|
Everything should be easy to undestand, but
seed -= int(seed);
should be explained...It just removes the integer part of the number, which leaves us with a number between 0 - 1
quote:
I think most random number generators just do something like this:
const int primenumber1 = some large prime number;
const int primenumber2 = some different prime number;
int GetRandom()
{
static int Seed;
Seed = (Seed + primenumber1) % primenumber2;
Return Seed;
}
But wont that generate a "stream" which looks something like this:
1 - 5 - 9 - 13 - 17
in other words: wont the seed increase with the same amount all the time???
delete this;
|
This is a snippet from my code, which is adapted from the code in Game Programming Gems. Basically you choose a large number ''m_seed'' and another large number ''m_multiplier''. Then you either use the raw Random() function or the templatized function which returns a number in the range [min, max]. (For [min, max) change 0xFFFFFFFF to 0xFFFFFFFE.) I am not sure how random this really is but it seems pretty nice.
Dirk =[Scarab]= Gerrits
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement