Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Generating random 6 digit numbers

by abell (Chaplain)
on Aug 26, 2002 at 13:32 UTC ( [id://192854]=note: print w/replies, xml ) Need Help??


in reply to Generating random 6 digit numbers

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://192854]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-03-28 08:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found