NXC random numbers / random seed

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: NXC random numbers / random seed

Post by muntoo »

m-goldberg wrote:Yes, you will always get a different sequence of pseudo-random numbers from NXC since srand is continuously being called with the current system time. There is, I suppose, a vanishing small probability the you could get the same sequence by reseting the system clock (by turning the NXT off and then on again) but you certainly couldn't depend on it :-)
What if you named the program "! Startup.rxe"? (Or whatever it was.)
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC random numbers / random seed

Post by HaWe »

that won't help in any way, muntoo.
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: NXC random numbers / random seed

Post by mattallen37 »

Actually, I believe that Muntoo means, that if your program starts running at exactly the same tick count every time (as if your program started automatically when the NXT was turned on, because the program was named "! Startup"), and if you base the seed only on the tick, you will always get the same results (assuming no timing change based on external differences). If needed, I would suggest adding other features as part of the seed. Perhaps battery level, ADCs... However, I do not see how that would ever, in reality, be an issue. I personally, very seldom name my program so it starts automatically, and I rarely use random numbers, not to mention that I have never used a random number generator that wasn't officially implemented (with seed based on who-knows-what).
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC random numbers / random seed

Post by HaWe »

the idea with !startup I still don't understand, but however - my random seed currently is based on CurrentTick()*Batterylevel().
I actually doubt that both startuptime and battery level will appear identically one after another.
If you start my demo program several times you'll observe that the seed will never be the same.
If it will appear nevertheless that a seed comes twice or even more often, it will be possible to combine it with any a current sensor reading (US, Light, Sound,...)
But much more important for test and evaluation purposes is the following:
1) now you may have constant seeds for identical pseudo random sequences (if you need to have it)
2) it still has to be proved that all drawn numbers of the generated random sequence are strictly stochastically independend (no cluster, no repetitive loops, all subsequences must be uniformly distributed in large samples): this is the most difficult condition, and I'm not quite sure about that (neither concerning the Lego rand algorithm nor concerning Andy's modulo series - in this respect the Mersenne-Twister algorithm seems to be the most promising way).
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: NXC random numbers / random seed

Post by muntoo »

doc-helmut wrote:the idea with !startup I still don't understand, but however - my random seed currently is based on CurrentTick()*Batterylevel().
I think the FW srand() is probably using CurrentTick(), but I don't know exactly. I was just replying to what Morton said.
Haha, look I changed your name :lol: :D :) wrote: I actually doubt that both startuptime and battery level will appear identically one after another.
Plausible. But I think at least 1 out of 100 will be the same as another seed. Maybe.

--

Of course, the Mersenne Twister is a great idea (I tried implementing it 2 years ago, but I was too dumb back then). You should consider adding a rand_init() which takes care of all the seeding for beginners.

Code: Select all

unsigned long RAND_SEED = 1;  // OR: CurrentTick() * BatteryLevel();


unsigned long mixTheBits(unsigned long in)
{
    unsigned long out = 0x00000000;
    for(byte bit = 0; bit < 0x20; bit++)
    {
        out |= (in >> (bit ^ 13)) << bit; // unlucky!
    }
    return(out);
}


unsigned long s_gen()
{
    return(CurrentTick() * BatteryLevel());

    // getting carried away...

    return(mixTheBits(CurrentTick() * BatteryLevel()));
}

void rand_init()
{
    srand(s_gen());
}
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: NXC random numbers / random seed

Post by mattallen37 »

muntoo wrote:
doc-helmut wrote:...my random seed currently is based on CurrentTick()*Batterylevel().
I think the FW srand() is probably using CurrentTick(), but I don't know exactly. I was just replying to what Morton said.
Haha, look I changed your name :lol: :D :) wrote: I actually doubt that both startuptime and battery level will appear identically one after another.
Plausible. But I think at least 1 out of 100 will be the same as another seed. Maybe...
A lot less than 1 out of 100. I am not sure how many bit the tick counter is, but it is probably at least 16 or 32 bits. That means that there are something like 65,000 (for 16 bit) tick count possibilities. If you multiply that by the operating voltage range, in mili-volts (about 3,600 - 10,000, so about 6,300), you get quite the number of possibilities. If you were to pass the multiplication of the two into a 16 bit int however, you still only have 16 bits to contain the number, so still only about 65,000 possibilities. That is much less than a 1% chance you will get the same seed. If for instance, you were to pass the seed number into an 8 bit variable (byte), there would be a one in 256 chance of a repeat out of two seeds.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC random numbers / random seed

Post by HaWe »

Code: Select all

out |= (in >> (bit ^ 13)) << bit; // unlucky!
what do yo mean with "unlucky" ?
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 2 guests