Home made Random Function?
Blitz3D Forums/Blitz3D Programming/Home made Random Function?
| ||
I'm messing about with a simple procedural routine but would like a specific seed to generate the same random number on everyones machines. I know there are known issues. Has anyone created their own function which can be used in a similar was to the native version? Even if you couold point me in the direction of convertable code/theory to create one myself? I'd like to avoid DLL's completely. Cheers Stevie |
| ||
Google for 'Mersenne Twister'. That should throw up plenty of code to pick over. |
| ||
well...SeedRnd "myspecialseed" Print Rnd(0,1) Print Rnd(0,1) Print Rnd(0,1) should print out exactly the same numbers on EVERY machine, assumed all compiling with the same blitz-version! on mine with blitz3d (and 32bit cpu) i get: 0.736565 0.3433 0.122169 try it out! |
| ||
There was problems with this i believe. Morduun came up with a proper seed function... somewhere... |
| ||
http://www.blitzbasic.com/Community/posts.php?topic=41909 if you look at the last post there, someone has done a lib for blitz3d/plus and MAX. I hope that helps. |
| ||
I thought there was a problem with this, and I remember Morduun complaining about it all the time, but OJay's example gave me the same results, so what is the actual problem with the random seed? I thought it had to do with different machines treating the same seed differently, but in that code ^^^ it seems to work properly, so now I'm confused. |
| ||
Yeah, I get the same results too but I was under the same impression as Pongo as it's been discussed quite a few times in the past 2-3 years. I'll check out Morduun's stuff and the Mersenne Twister. Cheers Guys :) |
| ||
A simple solution, definitely not the best, is the following which I have used on occasion - create a file called 'random.txt' decide what precision you want with your random numbers, perhaps a byte (0-255) for instance, write a few thousand values from 0-255 generated at random and store in your file 'random.txt'. The above steps are only needed to be done once and never again after that. In your game/app read these values, from the file, into an array, bank or type whichever suits best. Assuming for the moment you went with 1000 values, then you can make a simple function similar to 'seedrnd millisecs()' by setting the first random number to be generated in your sequence = millisecs() mod 1000 (same value between 0-999). Make sure you store the 'seed' used if you are intending for a replay of the game or similar. From that point, after generating your starting point simply iterate through the list of numbers, incrementing the array index by one each time and looping around when you reach the maximum. Of course, this means that your 'random sequence' will repeat after you've gone through all the numbers in the sequence but I would think it highly unlikely it would be noticed. As long as you start from a different point in the list initially it's hardly going to matter. As I said, not a great solution but it is easy and allows for the same simulation to take place each time if given the same seed on each person's machine since it uses the same data file as a reference. |
| ||
Ok, now I'm super curious as to what the problem really is. I tested this on 2 different machines and got the same result.SeedRnd 1000 For x = 1 To 10 Print Rand(0, 1000) Next Print SeedRnd "myseed" For x = 1 To 10 Print Rand(0, 1000) Next WaitKey() and the results were The interesting thing to me is that this appears to be working properly, BUT,... I took that code from an earlier thread (linked above) and it had given different results than what was listed there. (maybe an older version of blitz?) Can anyone enlighten? |
| ||
SeedRnd "myseed" (or any String that starts not with a number) is the same as SeedRnd 0 Strings that begin with Numbers are converted to numbers SeedRnd "3565" is the same as SeedRnd 3565 So, using Strings with SeedRnd is rubbish and only confusing beginners... I never met the situation when SeedRnd produced different lines of numbers on lots of different computers. I read years ago that there were some series of Intel CPUs that produced errors rounding numbers ... so they would change the line of numbers compared to other CPUs ... but this was ages ago ... . |
| ||
Mark has explained this but I can't remember exactly what the problem is. It's something to do with him using a FP routine and the FPU state not being guaranteed across multiple CPUs...or something... Do a forum search for more details. |
| ||
I thought the problem was that different numbers were sometimes generated across different CPUs? Something like that, anyway. |
| ||
Ok, now I'm super curious as to what the problem really is. I tested this on 2 different machines and got the same result. Mordy posted a test which was run on an awful lot of machines. Trust me, it's not reliable, and Skidracer himself confirmed it, if memory serves. You'll get the same results from most of the people, but not all the people. When you're using it for procedural content generation, or fractal seeding, you need it 100% and it's just not. |
| ||
ahhhh, got it. Just thought it was a bit odd, since I had seen a lot of posts about it, and then it appeared to be working. |