Making Rnd more random.

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.

Viewing 15 posts - 1 through 15 (of 17 total)
  • Author
    Posts
  • #5286

    cocon
    Participant

    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.

     

    Only two noticeable problems exist:

    1. 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.
    2. 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.

    #5288

    codifies
    Participant

    set the random seed to a value derived from the time

    #5290

    cocon
    Participant

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

    #5291

    codifies
    Participant

    after realising that “just get the time” is probably not as straight forward as could be expected….

    #5292

    codifies
    Participant

    now: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….

    #5293

    codifies
    Participant

    @cocon isnt millisecs since the start of the app ?

    #5297

    therevills
    Participant

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

    #5300

    codifies
    Participant

    I’m used it for just this in C apps and its worked just fine, I see no reason why this should be any different

    #5301

    codifies
    Participant

    oh 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…

    #5302

    cocon
    Participant

    Good tips, thanks to all.

    #5320

    Mark Sibly
    Keymaster

    I think there may be something a bit weird going on with Rnd, will take a look today.

    #5454

    cocon
    Participant

    Perhaps using a wrapper around the C standard library might make lot sense, I find no other better. 🙂

    http://www.cplusplus.com/reference/cstdlib/rand/

    #5606

    dubbsta
    Participant

    dont know if this would help, he explains randoms

    http://natureofcode.com/book/introduction/

    #5687

    EdzUp
    Participant

    I 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 :).

    #5725

    Mark Sibly
    Keymaster

    Uploaded 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:

    http://xoroshiro.di.unimi.it/

    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?

Viewing 15 posts - 1 through 15 (of 17 total)

You must be logged in to reply to this topic.