About Monkey 2 › Forums › Monkey 2 Development › Making Rnd more random.
This topic contains 16 replies, has 6 voices, and was last updated by 
 codifies
 2 years, 4 months ago.
- 
		AuthorPosts
 - 
		
			
				
November 21, 2016 at 6:35 am #5286
After getting weird results in the code I tried to see what the output of the Rnd would look like. I typed 10 times Print(Rnd(1, 10)) and saved the results.
1234567891011121314151617181920212223242526272829303132333435First Run1.00001034334951491.00005107970309536.7656760846835421.00005110558277977.51115708912147544.1448496539655269.04941257308458621.18974176156995488.38985945690603431.0045177719706286Second Run1.00001034334951491.00005107970309536.7656760846835421.00005110558277977.51115708912147544.1448496539655269.04941257308458621.18974176156995488.38985945690603431.0045177719706286Third Run1.00001034334951491.00005107970309536.7656760846835421.00005110558277977.51115708912147544.1448496539655269.04941257308458621.18974176156995488.38985945690603431.0045177719706286Only two noticeable problems exist:
- The number always starts with the lowest range at least two times, this is very important because when you have a play area and you initialize the object on the scene, it makes it very noticeable placing it in the corner all of the time.
 - The numbers seem to be repeated in a pattern at every application startup.
 
Workarounds
I tried testing SeedRnd but it looked like it did not work effectively it always brings numbers closer to 1.x
Also I tried this one to exhaust the results a little bit in the beginning, it’s not surely the most clever one but it would work for now.
Monkey123For Local i := 1 Until 10000Rnd(1, 10)NextNovember 21, 2016 at 6:47 am #5288set the random seed to a value derived from the time
November 21, 2016 at 6:57 am #5290Is it like? SeedRnd(Millisecs())
However I don’t know if you type it on Main function the application elapsed milliseconds would be 0, perhaps on somewhere else like loading a new scene or level initialization might make sense better, right?
November 21, 2016 at 7:13 am #5291after realising that “just get the time” is probably not as straight forward as could be expected….
Monkey1234567891011121314#import "<std>"#import "<libc>"Using std..Using libc..Function Main()Local tv:timevalgettimeofday( Varptr(tv) )Local t:Long = tv.tv_sec * 1000 + tv.tv_usecSeedRnd(t)Print Rnd(0,1)End FunctionNovember 21, 2016 at 7:20 am #5292now:ulong() returning milliseconds since the unix epoc might be a nice thing to have but what module would it live in?
std I guess but it is a but crowded….
November 21, 2016 at 7:22 am #5293November 21, 2016 at 7:52 am #5297Yes Millisecs is since the start of the app:
std:std.time.Millisecs
Function Millisecs:Int( )
Gets the number of milliseconds since the app started.Is gettimeofday okay to use?
November 21, 2016 at 8:43 am #5300I’m used it for just this in C apps and its worked just fine, I see no reason why this should be any different
November 21, 2016 at 8:44 am #5301oh wait you might want to double check how its implemented in windows, i assume the mingw compiler tools come with a windows implementation of gettimeofday and not just a stub…
November 21, 2016 at 10:53 am #5302Good tips, thanks to all.
November 21, 2016 at 6:13 pm #5320I think there may be something a bit weird going on with Rnd, will take a look today.
November 27, 2016 at 10:26 am #5454Perhaps using a wrapper around the C standard library might make lot sense, I find no other better.
http://www.cplusplus.com/reference/cstdlib/rand/
December 6, 2016 at 4:51 am #5606December 9, 2016 at 4:43 pm #5687I could port Ultim.Random system I have which is a port of Shagwanas random system if it helps
Uploaded a module for to the modules system that allows for random number generation, this is a port of Shagwana’s code on the BlitzMax forum (www.blitzbasic.com) so I have credited it to him and the porting to me :).
December 11, 2016 at 11:06 pm #5725Uploaded a module for to the modules system
I didn’t publish this sorry because:
a) Module name ‘random’ is too generic, use ‘edzup-random’ or something.
b) Hows does it compare with current rnd? If it’s better, perhaps current rnd needs replacing/fixing. If it’s worse what’s the point having it there at all? Even if there’s a point to having multiple rnd number generators (is there?) I think it’s still worth having 1 really good one in std.
I have tweaked the current std.random generator a bit – it now uses an algorithm called xoroshiro128+ which sounds like one of the best out there for speed vs quality:
Mersenne generators sounds like they were superceded a couple of years backs, and the ‘C’ rand() function has terrible reputation in general…
But I don’t *really* know the above for sure. I’m not any kind of expert on random numbers and it’s a very complex field, so perhaps what we need to do at some point is some actual emperical testing?
 - 
		AuthorPosts
 
You must be logged in to reply to this topic.