Board index FlightGear Development Nasal

Reproducible pseudorandom number sequence  Topic is solved

Nasal is the scripting language of FlightGear.

Reproducible pseudorandom number sequence

Postby Tomaskom » Tue Mar 03, 2015 11:01 pm

Hi,
I was trying to play a bit with the Nasal rand() random number generator, but I found no way of forcing it a seed of my choice thus receiving a predictable sequence. The motivation is an AI scenario, where it would generate objects on random locations, but the same for multiple people flying and searching together over MP. But at the same time, those flying should have no way of knowing the locations in advance, the seed would be announced when the flight starts.
This documentation says rand() generates random number in case of no argument (which it does), but sets a seed when there is an argument, returning nil in such case (which it doesn't). The behavior I observed with argument was exactly the same as with no argument, meaning it returns a random number, also the sequence of numbers didn't repeat after calling rand(seed) suggesting the seed wasn't actually set.
This wiki page says nothing about this, but specifies a srand() command to set the seed to current time. When giving srand() an argument, no sequence repetition is observed as well. Nothing suggests srand() should receive an argument, I just tried.

Is the FG Nasal not honoring the specification as described in the first link? Maybe some FG internal stuff is just polling the random numbers often so I never receive the same sequence for this reason, but I doubt it since calling rand(seed) with an argument should return nil based on the specification, not another random nubmer.
Is there any other straightforward way of generating a reproducible pseudorandom sequences? Implementing a simple PRNG in Nasal myself is not straightforward because all numbers are internally doubles.

A command specifying a PRNG object separate from the standard internal PRNG would be also nice (as no other code could then poll numbers from it influencing the sequence), but that is definitely too much to ask from a by-design simple and small scripting language.

The only way I see to achieve this with minimal work is probably to use the builtin md5() hash function, somehow translating the hash to a number. Quality of the randomness is not an issue. [EDIT]: Decoding the hash string (or a part) as hexadecimal value would be possible and easy with the utf8.strc(string,index); function, so solution, even while obscure, is found :)
"There are no problems in the air. The only problem is hitting the ground"

Get my L-159 ALCA from the FGUK hangar. If you crash it, things gonna go boom!
User avatar
Tomaskom
 
Posts: 132
Joined: Sun Dec 02, 2012 9:03 pm
Location: Czech Republic
Callsign: OK-TomK
Version: git
OS: openSUSE (Linux)

Re: Reproducible pseudorandom number sequence  

Postby Thorsten » Wed Mar 04, 2015 6:55 am

Is there any other straightforward way of generating a reproducible pseudorandom sequences? Implementing a simple PRNG in Nasal myself is not straightforward because all numbers are internally doubles.


I am almost certainly not getting the point - why is that a problem?

If quality of randomness isn't an issue, there's a whole host of one-liners you can google. Basically, you call a very complicated and rapidly varying non-linear function and truncate it by using only the fractional part of it - there's your random number.

Otherwise you just keep iterating to generate a sequence.

As an example, that's the random number function which underlies all the noise distributions in our shader code - as you can see, it's not particularly involved to do:

Code: Select all
float rand2D(in vec2 co){
    return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
}
Thorsten
 
Posts: 12490
Joined: Mon Nov 02, 2009 9:33 am

Re: Reproducible pseudorandom number sequence

Postby Tomaskom » Wed Mar 04, 2015 11:07 am

Thank you, the problem was I was working only with applications where high quality uniform generators were required so far, so I didn't know such simple approach could be used when the requirements are loose.
This is exactly the kind of answer I was hoping for :)
"There are no problems in the air. The only problem is hitting the ground"

Get my L-159 ALCA from the FGUK hangar. If you crash it, things gonna go boom!
User avatar
Tomaskom
 
Posts: 132
Joined: Sun Dec 02, 2012 9:03 pm
Location: Czech Republic
Callsign: OK-TomK
Version: git
OS: openSUSE (Linux)


Return to Nasal

Who is online

Users browsing this forum: No registered users and 2 guests