Alternative for Randomize Timer

General FreeBASIC programming questions.
deltarho[1859]
Posts: 1914
Joined: Jan 02, 2017 0:34
Location: UK

Alternative for Randomize Timer

Postby deltarho[1859] » Feb 25, 2019 1:13

I spotted this in some code by Steve Hutchesson at the PowerBASIC forums and wrote a Sub wrapper for it.

The default algorithm is for the current language dialect and so is 3 for -lang fb.

Code: Select all

Sub RandomSeed( Byval algorithm as Byte = 0 )
Dim As Ulong Seed
  Asm
    rdtsc ' Get a number from the processor
    bswap eax ' invert to get the fast changing end
    mov dword Ptr [Seed], eax ' store it in Seed
  End Asm
Randomize Seed, algorithm
End Sub
 
' Example usage
RandomSeed
For i As Ulong = 1 To 10
  Print Rnd
Next
 
Sleep
deltarho[1859]
Posts: 1914
Joined: Jan 02, 2017 0:34
Location: UK

Re: Alternative for Randomize Timer

Postby deltarho[1859] » Feb 27, 2019 12:52

I looked at a variant as follows

Code: Select all

Sub RandomSeed( Byval algorithm as Byte = 0 )
Dim As Ulong Seed
  Asm
    rdtsc ' Get a number from the processor
    mov ebx, eax ' copy eax
    bswap eax ' invert to get the fast changing end
    xor eax, ebx
    mov dword Ptr [Seed], eax ' store it in Seed
  End Asm
  Randomize Seed, algorithm
End Sub

Executing many RandomSeeds in quick succession and printing them gave a random looking distribution. Totalling 10^8 of them and normalizing by dividing by 2^32 gave an average closer to 0.5 that I have ever seen. Of course, that in of itself, does not prove good quality random numbers but not getting close to 0.5 proves non-randomness.

However, when I streamed the 32-bit seeds into PractRand it failed spectacularly at 1KB. So, the 32-bit seeds generated are not even close to being random.

It then dawned on me that we would be hard pressed to do better than the following:

Code: Select all

Sub RandomSeed( Byval algorithm as Byte = 0 )
Randomize , 5
Dim As Ulong Seed = Cast( Ulong, Rnd*(2^32) )
  Randomize Seed, algorithm
End Sub

We cannot do this so easily in PowerBASIC because it does not have a cryptographic generator (#5) built in.

<smile>
jj2007
Posts: 1214
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Alternative for Randomize Timer

Postby jj2007 » Feb 27, 2019 18:27

Looks ok, but is it really necessary to have good randomness of the seed? You typically use it once in a program, then you produce Millions of really random numbers with e.g. PCG32
deltarho[1859]
Posts: 1914
Joined: Jan 02, 2017 0:34
Location: UK

Re: Alternative for Randomize Timer

Postby deltarho[1859] » Feb 27, 2019 19:05

jj2007 wrote:but is it really necessary to have good randomness of the seed?

I imagine the period of a PRNG to be a circle comprising a sequence. Randomize n, where n is fixed, gives us one entry point to the circle and the random numbers generated will be the same for each invocation of Randomize n. n as a variable gives us n entry points to the circle. Ideally, the values of n should be equally likely. For a 32-bit seed, the best value will be a cryptographic Ulong - unpredictable and equally likely. PCG32II uses a cryptographic Ulongint. My RndMT uses 624 cryptographic Ulongs, for a system seed, to populate the state vector as opposed to a 32-bit seed and a LCG; the latter giving only 4Gigs of entry points.

Added: BTW, I have Intel RdRand on my CPU but I do not have RdSeed, which was introduced on later chips. Intel says "RDSEED is intended for seeding a software PRNG of arbitrary width. RDRAND is intended for applications that merely require high-quality random numbers." For a longer description see "The long answer" here.
deltarho[1859]
Posts: 1914
Joined: Jan 02, 2017 0:34
Location: UK

Re: Alternative for Randomize Timer

Postby deltarho[1859] » Mar 10, 2019 9:09

jj2007 wrote:but is it really necessary to have good randomness of the seed?

I decided to search the Internet for confirmation of my response and found a consensus of comments to the contrary saying that a random seed is not necessary - uniqueness is though.

Using a cryptographic seed is OK as it satisfies the uniqueness aspect but being equally likely is not necessary.

Thansks, jj.

Return to “General”

Who is online

Users browsing this forum: No registered users and 3 guests