Random Number
Dan Bishop
danb_83 at yahoo.com
Sat Mar 27 19:55:39 EST 2004
talktojamesblair at yahoo.com (james blair) wrote in message news:<a4648036.0403271156.787706e5 at posting.google.com>...
> Hi
> I am generating a random number using
> random.randint(1,10000000000)
> Whats the possibility that the numbers generated will be same when
> generated by 100 users at the same time?
The probability that all 100 numbers will be the same is 1e-1000. But
you probably wanted the probability that *any* two numbers will be the
same, which is approximately 4.95e-07, or about 1 in 2 million.
> Whats the best method to generate random numbers so that they are most
> likely unique??
If the probability of collisions is low, use:
def UniqueRandom(a, b):
"Generator for random integers between a and b, inclusive."
alreadyUsedNumbers = sets.Set()
while True:
randomNumber = random.randint(a, b)
if randomNumber not in alreadyUsedNumbers:
alreadyUsedNumbers.add(randomNumber)
yield randomNumber
If the probability of collisions is high, use:
def UniqueRandom(a, b):
"Generator for random numbers between a and b, inclusive."
sampleSpace = range(a, b + 1)
random.shuffle(sampleSpace)
return iter(sampleSpace)
More information about the Python-list
mailing list