Re: Way for computing random primes in standard C.
On 2006-02-28, Flash Gordon <spam@flash-gordon.me.uk> wrote:[color=blue]
> Keith Thompson wrote:[color=green]
>> Jordan Abel <random832@gmai l.com> writes:[color=darkred]
>>> On 2006-02-28, Keith Thompson <kst-u@mib.org> wrote:[/color]
>> [...][color=darkred]
>>>> C99 7.20.2.2p2 says:
>>>>
>>>> The srand function uses the argument as a seed for a new sequence
>>>> of pseudo-random numbers to be returned by subsequent calls to
>>>> rand. If srand is then called with the same seed value, the
>>>> sequence of pseudo-random numbers shall be repeated. If rand is
>>>> called before any calls to srand have been made, the same sequence
>>>> shall be generated as when srand is first called with a seed value
>>>> of 1.
>>>>
>>>> Note the phrase "a new sequence". It's possible, but not required,
>>>> that the "new sequence" matches some other sequence at a different
>>>> position. It's also possible that it doesn't. For example, there may
>>>> be no overlap between the sequence created by srand(1) and the one
>>>> created by srand(2) (this is particularly likely if rand()'s internal
>>>> state has more bits than the unsigned int argument to srand()).
>>> Not quite - Let's suppose, naively, that srand() merely initializes a
>>> particular set of bits of rand()'s internal state, and sets the
>>> remaining bits to zero. Now, if at any point in srand(1)'s sequence, the
>>> internal state contains the result of srand(2) [reasonable if it's
>>> "perfect" i.e. cycles through every possible combination of bits the
>>> internal state could have], srand(1) and srand(2) are thus two different
>>> points in the same sequence.[/color]
>>
>> Yes, I think you're right.
>>
>> Suppose rand() has N bits of internal state. If it cycles through all
>> 2**N possible internal states, then you can think of the set of all
>> states as a closed loop with 2**N nodes; each call to rand() advances
>> on position along the loop, and each call to srand() jumps to a single
>> point on the loop. (Ideally the points for different values of
>> srand() are roughly equally spaced along the loop, maximizing the
>> uniqueness of each subsequence.)
>>
>> If the cycle is shorter than 2**N, then the states form some number of
>> disjoint loops rather than one single loop. A call to rand() would
>> advance by one position on a single loop, and a call to srand() could
>> jump to an arbitrary point on any loop.
>>
>> But as you point out, an RNG that cycles through its entire state is
>> probably better than one that doesn't (but one that doesn't is
>> certainly permitted by the standard).[/color]
>
> Here's an idea. Isn't Pi meant to be a non-repeating series of digits?
> See http://www.piworld.de/pi-statistics/ So how about for your PRNG
> calculating the digits of Pi in base RAND_MAX+1?
>
> srand could then just select where to start.
>
> This would not be good enough for security, since if anyone knows that
> it is producing the digits of Pi all they have to do is identify where
> in the sequence you are, but for other uses it should produce an
> infinite (as far as we know) sequence of numbers with very good
> statistical properties.
>
> It might also not be the most efficient method of producing random numbers.[/color]
If RAND_MAX+1 is a power of 2, it could be done (based on the fact that
a formula exists to independently calculate hex digits of pi)
You'd probably want srand() to initialize the upper bits of the state
[said state would be an index into the "digits" in that case] rather
than the lower ones.
[color=blue]
> Note that I am *not* an expert on statistics, Pi, or PRNGs.[/color]
So basically neither of us knows how good it would really be.
On 2006-02-28, Flash Gordon <spam@flash-gordon.me.uk> wrote:[color=blue]
> Keith Thompson wrote:[color=green]
>> Jordan Abel <random832@gmai l.com> writes:[color=darkred]
>>> On 2006-02-28, Keith Thompson <kst-u@mib.org> wrote:[/color]
>> [...][color=darkred]
>>>> C99 7.20.2.2p2 says:
>>>>
>>>> The srand function uses the argument as a seed for a new sequence
>>>> of pseudo-random numbers to be returned by subsequent calls to
>>>> rand. If srand is then called with the same seed value, the
>>>> sequence of pseudo-random numbers shall be repeated. If rand is
>>>> called before any calls to srand have been made, the same sequence
>>>> shall be generated as when srand is first called with a seed value
>>>> of 1.
>>>>
>>>> Note the phrase "a new sequence". It's possible, but not required,
>>>> that the "new sequence" matches some other sequence at a different
>>>> position. It's also possible that it doesn't. For example, there may
>>>> be no overlap between the sequence created by srand(1) and the one
>>>> created by srand(2) (this is particularly likely if rand()'s internal
>>>> state has more bits than the unsigned int argument to srand()).
>>> Not quite - Let's suppose, naively, that srand() merely initializes a
>>> particular set of bits of rand()'s internal state, and sets the
>>> remaining bits to zero. Now, if at any point in srand(1)'s sequence, the
>>> internal state contains the result of srand(2) [reasonable if it's
>>> "perfect" i.e. cycles through every possible combination of bits the
>>> internal state could have], srand(1) and srand(2) are thus two different
>>> points in the same sequence.[/color]
>>
>> Yes, I think you're right.
>>
>> Suppose rand() has N bits of internal state. If it cycles through all
>> 2**N possible internal states, then you can think of the set of all
>> states as a closed loop with 2**N nodes; each call to rand() advances
>> on position along the loop, and each call to srand() jumps to a single
>> point on the loop. (Ideally the points for different values of
>> srand() are roughly equally spaced along the loop, maximizing the
>> uniqueness of each subsequence.)
>>
>> If the cycle is shorter than 2**N, then the states form some number of
>> disjoint loops rather than one single loop. A call to rand() would
>> advance by one position on a single loop, and a call to srand() could
>> jump to an arbitrary point on any loop.
>>
>> But as you point out, an RNG that cycles through its entire state is
>> probably better than one that doesn't (but one that doesn't is
>> certainly permitted by the standard).[/color]
>
> Here's an idea. Isn't Pi meant to be a non-repeating series of digits?
> See http://www.piworld.de/pi-statistics/ So how about for your PRNG
> calculating the digits of Pi in base RAND_MAX+1?
>
> srand could then just select where to start.
>
> This would not be good enough for security, since if anyone knows that
> it is producing the digits of Pi all they have to do is identify where
> in the sequence you are, but for other uses it should produce an
> infinite (as far as we know) sequence of numbers with very good
> statistical properties.
>
> It might also not be the most efficient method of producing random numbers.[/color]
If RAND_MAX+1 is a power of 2, it could be done (based on the fact that
a formula exists to independently calculate hex digits of pi)
You'd probably want srand() to initialize the upper bits of the state
[said state would be an index into the "digits" in that case] rather
than the lower ones.
[color=blue]
> Note that I am *not* an expert on statistics, Pi, or PRNGs.[/color]
So basically neither of us knows how good it would really be.
Comment