POV-Ray : Newsgroups : povray.general : Random 8 digit number problem Server Time: 20 Jun 2019 06:56:04 GMT
  Random 8 digit number problem (Message 1 to 3 of 3)  
From: Anthraqunione
Subject: Random 8 digit number problem
Date: 19 Feb 2018 12:25:01
Message: <web.5a8ac107279b8aeacd4c57b30@news.povray.org>
Povray 3.7

I want an 8 digit random interger and used thisi code

#declare Random       = true;                   // User random number for image
#declare Seed         = 1001011;
#if (Random = true)
  #local R = seed (Seed);
  #declare S  = rand(R);
  #declare S = int(S*100000000);
#end

I started with Seed = 1 and was planning to work upwards as I expected each
final number the be "random".  I found the final 2 or 3 digits repeated and
started trying different values of Seed.  This is what I got

1               42199287
2               84398287
3               26597287

10              21990288
11              64189288

101             62099296
102             04298296

1000001         42286854
1000002         84485854

1001001         41286941
1001011         63276942

Are the above results what is expected ?.  It seems that only the first three
digits generated are anything like random

AQ


Post a reply to this message

From: clipka
Subject: Re: Random 8 digit number problem
Date: 19 Feb 2018 17:50:52
Message: <5a8b0e7c$1@news.povray.org>
Am 19.02.2018 um 13:21 schrieb Anthraqunione:
> Povray 3.7
> 
> I want an 8 digit random interger and used thisi code
> 
> #declare Random       = true;                   // User random number for image
> #declare Seed         = 1001011;
> #if (Random = true)
>   #local R = seed (Seed);
>   #declare S  = rand(R);
>   #declare S = int(S*100000000);
> #end
> 
> I started with Seed = 1 and was planning to work upwards as I expected each
> final number the be "random".  I found the final 2 or 3 digits repeated and
> started trying different values of Seed.  This is what I got

You're using the pseudorandom number generator wrong. Traditional
(non-cryptographic) pseudorandom number generators (such as the one in
POV-Ray) are designed to produce streams of numbers that have
pseudorandom properties /within that stream/, but not necessarily
/between streams/ with different seeds.

Especially the first numbers pulled from such a pair of such streams may
exhibit noticeable similarities when the seeds are similar.

To get any reasonable randomness out of the approach you're using, you'd
need to pull and discard the first few random numbers from the stream, e.g.:

    #declare Random = true;
    #declare Seed = 1001011;
    #if (Random = true)
      #local R = seed (Seed);
      #for (I,1,10)
        #declare Dummy = rand(R);
      #end
      #declare S = rand(R);
      #declare S = int(S*100000000);
    #end

However, the proper way to make use of POV-Ray's pseudorandom number
generator would be to use just one seed, and pull actual sequences out
of the generator; e.g.:

    #declare Random = true;
    #declare Seed = 1001011;
    #if (Random = true)
      #local R = seed (Seed);
      #for (I,1,100000)
        #declare S = rand(R);
        #declare S = int(S*100000000);
        // actually do something with S here
      #end
    #end

If you can't do this because you just need one random number for each
frame of an animation, possibly the fastest solution would be to skip
`frame_number` random numbers each time the scene is parsed, e.g.:

    #declare Random = true;
    #declare Seed = 1001011; // keep same for all frames
    #if (Random = true)
      #local R = seed (Seed);
      #for (I,1,frame_number)
        #declare Dummy = rand(R);
      #end
      #declare S = rand(R);
      #declare S = int(S*100000000);
    #end


If you want to stick to your approach, instead of seeding a pseudorandom
number generator and pulling a single value you should use a hash
function instead and apply it to the "seed" (CRC32 might do). You'll
have to program the has function yourself though, as there's none built
into POV-Ray.


Post a reply to this message

From: Anthraqunione
Subject: Re: Random 8 digit number problem
Date: 20 Feb 2018 11:45:00
Message: <web.5a8c09de9e0e52f6cd4c57b30@news.povray.org>
clipka <ano### [at] anonymousorg> wrote:
> Am 19.02.2018 um 13:21 schrieb Anthraqunione:
> > Povray 3.7
> >
> > I want an 8 digit random interger and used thisi code
> >
> > #declare Random       = true;                   // User random number for image
> > #declare Seed         = 1001011;
> > #if (Random = true)
> >   #local R = seed (Seed);
> >   #declare S  = rand(R);
> >   #declare S = int(S*100000000);
> > #end
> >
> > I started with Seed = 1 and was planning to work upwards as I expected each
> > final number the be "random".  I found the final 2 or 3 digits repeated and
> > started trying different values of Seed.  This is what I got
>
> You're using the pseudorandom number generator wrong. Traditional
> (non-cryptographic) pseudorandom number generators (such as the one in
> POV-Ray) are designed to produce streams of numbers that have
> pseudorandom properties /within that stream/, but not necessarily
> /between streams/ with different seeds.
>
> Especially the first numbers pulled from such a pair of such streams may
> exhibit noticeable similarities when the seeds are similar.
>
> To get any reasonable randomness out of the approach you're using, you'd
> need to pull and discard the first few random numbers from the stream, e.g.:
>
>     #declare Random = true;
>     #declare Seed = 1001011;
>     #if (Random = true)
>       #local R = seed (Seed);
>       #for (I,1,10)
>         #declare Dummy = rand(R);
>       #end
>       #declare S = rand(R);
>       #declare S = int(S*100000000);
>     #end
>
> However, the proper way to make use of POV-Ray's pseudorandom number
> generator would be to use just one seed, and pull actual sequences out
> of the generator; e.g.:
>
>     #declare Random = true;
>     #declare Seed = 1001011;
>     #if (Random = true)
>       #local R = seed (Seed);
>       #for (I,1,100000)
>         #declare S = rand(R);
>         #declare S = int(S*100000000);
>         // actually do something with S here
>       #end
>     #end
>
> If you can't do this because you just need one random number for each
> frame of an animation, possibly the fastest solution would be to skip
> `frame_number` random numbers each time the scene is parsed, e.g.:
>
>     #declare Random = true;
>     #declare Seed = 1001011; // keep same for all frames
>     #if (Random = true)
>       #local R = seed (Seed);
>       #for (I,1,frame_number)
>         #declare Dummy = rand(R);
>       #end
>       #declare S = rand(R);
>       #declare S = int(S*100000000);
>     #end
>
>
> If you want to stick to your approach, instead of seeding a pseudorandom
> number generator and pulling a single value you should use a hash
> function instead and apply it to the "seed" (CRC32 might do). You'll
> have to program the has function yourself though, as there's none built
> into POV-Ray.

Thank you for that.  For me the first idea works well.  I just need a completly
different set of 8 parameters each time I run the scene file.

I think the use of "seed" and "rand" could be explained better in the help file
which is where I got my original code from.


Post a reply to this message

Copyright 2003-2008 Persistence of Vision Raytracer Pty. Ltd.