We don't bite newbies here... much | |
PerlMonks |
Re: Generating random 6 digit numbersby abell (Chaplain) |
on Aug 26, 2002 at 13:32 UTC ( [id://192854]=note: print w/replies, xml ) | Need Help?? |
One of the simplest pseudo-random generators uses consecutive powers of an integer modulo a prime p. If the basis integer is well chosen, then you get all numbers modulo p (apart from 0) before a repetition.
For instance, taking p=7 and 3 as basis, one gets
3^1 = 3 = 3 (mod 7) 3^2 = 9 = 2 (mod 7) 3^3 = 27 = 6 (mod 7) 3^4 = 81 = 4 (mod 7) 3^5 = 243 = 5 (mod 7) 3^6 = 729 = 1 (mod 7)The outcome is a shuffle of the numbers (1..6). In your case, you could choose 999983 as p, wasting 17 possible numbers, or 1000003, which forces you to repeat the generation when the outcome is greater than 999999. In both cases, a number only depends on the previous one, which makes generation very easy and storage minimum. If you depend on an attacker not being able to guess the sequence, this method is far too weak. For your convenience, you could use 530225 for integers modulo 999983 and 318611 for integers modulo 1000003 Best regards Antonio Bellezza
In Section
Seekers of Perl Wisdom
|
|