Multiplatform Random Number Consistancy

BlitzMax Forums/BlitzMax Programming/Multiplatform Random Number Consistancy

RexRhino(Posted 2004) [#1]
I am wondering if random numbers are consistant across multiple platforms? If I use them in hash functions, to generate random levels, etc., can I be sure the numbers will be the same on all platforms (i.e. the random number is handled by Blitz code and not the respective code on each platform)? Has anyone done any testing on this?


Warren(Posted 2004) [#2]
I don't know for sure but I highly doubt that they will be consistent across multiple platforms/processors. I would be amazed if they are...


John Pickford(Posted 2004) [#3]
Why not?


Warren(Posted 2004) [#4]
Are you seriously suggesting that every OS/processor/platform uses the same random number generator?


Dreamora(Posted 2004) [#5]
Hashes don't have random numbers normally ( they base on primes normally) so they are should be consistent.

why should randoms be consistent? their use is to be random not to be simple precalculatable ;)


Michael Reitzenstein(Posted 2004) [#6]
why should randoms be consistent? their use is to be random not to be simple precalculatable ;)

What are the SeedRnd and RndSeed commands for, then?


Kanati(Posted 2004) [#7]
Are you seriously suggesting that every OS/processor/platform uses the same random number generator?


If wrote the generator (or used someone elses) and it's not using the standard C library (which might be different across C compilers/platforms) then it should be fine. Otherwise......... Not so much.


John Pickford(Posted 2004) [#8]
Are you seriously suggesting that every OS/processor/platform uses the same random number generator?


Yes. Or rather I'm suggesting Mark wrote a random number generator that behaves the same way on every CPU. Not THAT hard to do.


bradford6(Posted 2004) [#9]
I always thought of "random" as unpredictable. If you want to predict a dataset, why not create it using a different algorithm


Jim Teeuwen(Posted 2004) [#10]
I agree. Ive had this discussion with Morduun a few times. I really dont see why random numbers should be predictable. The very meaning of the word RANDOM is UNpredictable.


ran·dom ( P ) adj.
1. Having no specific pattern, purpose, or objective: random movements. See Synonyms at chance.
2. Mathematics & Statistics. Of or relating to a type of circumstance or event that is described by a probability distribution.
3. Of or relating to an event in which all outcomes are equally likely, as in the testing of a blood sample for the presence of a substance.




Kanati(Posted 2004) [#11]
But when it comes to computers there's no such thing as random. You seed the generator with 8283452 and you'll return the same set every single time. The same goes for any other seed.

So lets say you want to make a multiplayer game... And you want it to be random... But instead of sending the events across the line... you just seed both machines' random generators with the same seed. Now you don't have to send those events. You have both machines having the same randomness across the board. You know that the third random number you pull on the mac player's machine will be the exact same random number you pull on the pc machine.

Probably not a good example as I'm not sure you'd want to handle things like that, but that's just one possible reason for non-random random numbers.

Kanati


jhague(Posted 2004) [#12]
Also, it is rare for a random number generator to be truly random. Some of them are random for some purposes, but exhibit patterns when used for others. I think what people don't want is a random arrangement of (let's say) bad guys on the screen that looks great under Windows but tends to have a less random appearance under OS X, for example.

To elaborate here, some random number generators pass test for randomness for long sequences, but not short sequences. So your "random" placement of enemies commonly ends up with visible clumps.


John Pickford(Posted 2004) [#13]
I think it's well understood that a random number generator returns a stream of psuedo-random numbers (with even distribution). The stream can be seeded so a predictable stream can be generated. This is great for generating game levels and worlds which need to be the same each time. Famous example: Elite.

Truly random numbers would be arguably less useful.


ImaginaryHuman(Posted 2004) [#14]
I was playing with a routine recently to bounce some objects around the screen and of course, to intialize their positions I just do a loop with random numbers for X and Y. Thing is, I later noticed that with a small number of objects they all seemed to start out in the same place every time. Then I figured it must be because the seed number stays the same unless I change it - even accross multiple compiles!

From what I gather of what the original question is asking, I was thinking the same kinda thing. See, if you want to have a random level generator in a game, let's say it puts walls in random positions or adds powerups in random locations, etc, there might be some reason why you want to then later `keep` the way that level was set up so you can come back to it if it happened to be a good one. If you knew what seed the random numbers were based on you could have it then re-generate the same results later on using that same seed number. Then you could for example just save the seed number to refer to the layout of the level. In a lot of games you may want to have the player go back to a previously created `level` to replay it (although that slightly goes against the purpose of making levels randomly, it might be a good part of the game structure).

Also yes, there are no truly random numbers in computers unless maybe you figure in some real-world input such as the time of day or whatever, which is likely to be at least slightly different each time. All numbers generated based on that seed are then different. Of course, you could have multiple re-seeding (hairline?) in the program to really produce a lot of variations.

For me, I will be creating random levels where just about everything in the level is computer-generated, but I also want to include in the game some `pre-made` levels which happen to be good ones (after all, some random results are better than others unless you specifically add in some sophisticated template or guidance) ... and I want to use the same random-level generator to generate the levels for me so that I don't have to take 50 years making a level. I can just generate a level, decide if it's a good one, and move onto the next one, keeping if it gets past my keen eye. Then it can be regenerated the same later on without having to store and load lots of level data etc. I'm sure this guy is trying to do something similar.

As for cross platform compatibility, I really don't know what algorithm they use. Maybe if you want to be sure, look on the web or something and find a random-number generator program and use it in your game to make your own randon numbers, then you have full control of the process.


Warren(Posted 2004) [#15]
John

If Mark wrote the random number generator himself, then I agree - it would/should always be the same.



Can anyone from BRL confirm or deny this?


FlameDuck(Posted 2004) [#16]
I really dont see why random numbers should be predictable.
Because you're asking a computer (that is a Finite State Machine) to generate them. Thus it is impossible to get truely random numbers. The only reason they appear random, is because we aren't good enough at large scale pattern matching.

Also yes, there are no truly random numbers in computers unless maybe you figure in some real-world input such as the time of day or whatever, which is likely to be at least slightly different each time.
Even then it would still not be entirely random.

Still isn't it as easy to test as try the same program on a Mac and PC and see what the results are?


Kanati(Posted 2004) [#17]
From what I gather of what the original question is asking, I was thinking the same kinda thing. See, if you want to have a random level generator in a game, let's say it puts walls in random positions or adds powerups in random locations, etc, there might be some reason why you want to then later `keep` the way that level was set up so you can come back to it if it happened to be a good one. If you knew what seed the random numbers were based on you could have it then re-generate the same results later on using that same seed number. Then you could for example just save the seed number to refer to the layout of the level. In a lot of games you may want to have the player go back to a previously created `level` to replay it (although that slightly goes against the purpose of making levels randomly, it might be a good part of the game structure).


SSI had a dungeons and dragons gold box game like Hack or Moria... Thinking it was called Ulimited Adventures or something like that. I am almost sure it did exactly that. It seeded it's random number generator to create the dungeons. AND... It gave that number to the user. So if you wanted your friend to run through the exact same dungeon that you ran through, all you had to do was write down that "seed" and your friend could plop that number into his game and play the same dungeon. :)


Robert(Posted 2004) [#18]
Because you're asking a computer (that is a Finite State Machine) to generate them. Thus it is impossible to get truely random numbers. The only reason they appear random, is because we aren't good enough at large scale pattern matching.



Although it is not possible to generate random numbers using a PC, it is possible to calculate the randomness of a formula.


Todd(Posted 2004) [#19]
The following program prints the same random number on both Mac OS X and Linux, and both use two different C libraries. Which means that Blitz generates it's own random numbers. I haven't tested in Windows however, so I may be wrong.

SeedRnd 1000
Print "Random: " + Rnd(0, 1000) 
' Returns 89.911744965587289



ImaginaryHuman(Posted 2004) [#20]
Sounds good.

And yes, even if you include time of day as the seed the random numbers still spew out in a sequence. I'm just saying that if you add in some kind of real-world data (such as the time of execution) that helps keep it more differentiated.


ImaginaryHuman(Posted 2004) [#21]
I think I remember playing some game too on the Amiga where it generated levels based on a seed, oh .. hang on, I'm thinking of the original version of `vista` which had to have a seed in order to generate a given landscape. Same effect though - same seed, reproduced results. Actually works in your favor if you WANT the same results every time. It's when you want something more randomized that things get trickier.

For a game that creates random levels though I would think that it would at least have some kind of templates or rules about what `features` the randomly generated level needs to have, like no stupid dead ends or no `unfair` areas if it's multiplayer. I think that's where making a mix of some pre-thought design with randomness works best.


BlitzSupport(Posted 2004) [#22]
Anyone fancy porting the Mersenne Twister?

There's not much code to port, but I came unstuck with the unsigned integers! I know there are ways to work around such issues, but I've never been too hot with low-down bit-twiddling myself...

http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/ewhat-is-mt.html


RexRhino(Posted 2004) [#23]
The following program prints the same random number on both Mac OS X and Linux, and both use two different C libraries. Which means that Blitz generates it's own random numbers. I haven't tested in Windows however, so I may be wrong.

Thanks Todd, finally someone talking sense!!! :) That is exactly the type of thing I was looking for! I get the same result on my PC, except for the small decimal points...

Perhaps it would be better to compare a string of integers. How about people try this on different platforms:
SeedRnd 1000
For x = 1 To 10
	Print "Random " + x + ": " + Rand(0, 1000) 
next 


I get the following values:
Random 1: 90
Random 2: 410
Random 3: 823
Random 4: 904
Random 5: 625
Random 6: 191
Random 7: 919
Random 8: 675
Random 9: 536
Random 10: 847


As for people saying that random numbers should be truly random, that is crazy. Too many games demand consistant random numbers.


ImaginaryHuman(Posted 2004) [#24]
Also for most purposes, for me, computer-generated random numbers are random ENOUGH for most purposes.


EddieRay(Posted 2005) [#25]
FWIW, the SSI game with the random-generated dungeons, given a seed value, was called "Dungeon Hack" (not a gold-box game).

The MT random number generator code is probably the way to go to insure cross-platform random numbers. It uses integers for all calculations, and it appears to use accepted techniques for generating "nice" random numbers. It should be easy to compile the C code into a Blitz userlib/module. Or, you could dust off yer copy of Numerical Recipes in C and adapt one of the better algorithms to use integers. Fortunately, it's not too terribly hard. I remember doing this back in the 80286/386 days, just so I knew exactly how the generator functioned and could adapt it if needed (and it was nice to be able to optimize it for speed if needed - having too many float operations back then was very bad).

Even given the IEEE standards for floating point math, etc., and the standard nature of the "libc" random number generators like "random", "rand" and "lrand48", etc., my guess is that the results are not guaranteed to match up on different platforms (i.e. different CPUs with different floating point units and different standards compliance) But then again, maybe the standards are enough to ensure portability (one would think this is pretty important). Anyone well-versed in such "standards" issues?

Hope this helps,

Ed


jhague(Posted 2005) [#26]
Note that on the x86, floating point math is done with 80-bits internally, vs. 64 on the PowerPC. You'd need to use integer math to get rock solid consistency across platforms.


Kanati(Posted 2005) [#27]
The MT random number generator code is probably the way to go to insure cross-platform random numbers. It uses integers for all calculations, and it appears to use accepted techniques for generating "nice" random numbers. It should be easy to compile the C code into a Blitz userlib/module.


Already done... Although very few people seemed to care. I compiled a dll/userlib for blitz+ and blitz3D, and a mod for blitzmax... It's in the "programming" forums of the respective products.

Blitzmax Mod:
http://www.obsidianobelisk.com/software/mersenne.zip

Blitz+ and Blitz3D Userlib:
http://www.obsidianobelisk.com/software/mtuserlib.zip


Commands:

SeedRand(seed%) - seed the random number generator
x% = Rand32() - return the next number in PRNG
x% = RandMax(max%) - return random number from 1 - max
x% = RandRange(lo%, hi%) - return random number from lo - hi


Krischan(Posted 2008) [#28]
The links are dead :-( Kanati (oder someone else), can you please upload the two files again?


GfK(Posted 2008) [#29]
This thread is 3 years old - of course the links are dead.

Try Brucey's BAH.Random module instead.


Russell(Posted 2008) [#30]
Nothing in existence in the universe(s) is truly random, since every effect somewhere along the line has a cause even though we may never know what that cause is. Maybe when you get down to the sub-nuclear level and the unknown laws of quantum mechanics comes into play...

Russell


ImaginaryHuman(Posted 2008) [#31]
I agree nothing is totally random but things can appear to be `highly random`, or irregular.