• ### Announcements

#### Archived

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

# psuedorandom

## Recommended Posts

krez    443
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)

##### Share on other sites
Agent    122
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

##### Share on other sites
ragonastick    134
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;}

Sorry if that code is wrong syntax, the idea is there... C isn''t my native tongue

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.

##### Share on other sites
Dobbs    164
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.

##### Share on other sites
krez    443
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)

##### Share on other sites
Tjoppen    122
I feel nice today...

I´ll just give way my generator again:

  float seed = some_number;float rand(void){ seed += PI; seed *= seed; seed -= int(seed); return seed;}void srand(float s){ seed = s;}

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;
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???

  unsigned int CRandomizer::Random(){ m_seed = m_multiplier * (m_seed + 2); return m_seed;} // Generate a random number between min and maxtemplate T CRandomizer::Random(const T min, const T max){ return min + T ( double(Random()) * double(max-min) / double(0xFFFFFFFF) );}