473,660 Members | 2,459 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Random Number

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?
Whats the best method to generate random numbers so that they are most
likely unique??
Thanks
Jul 18 '05 #1
4 3407
The probability that the same number will turn up more than once is
approximately one in two million. If you're still worried, you might want to try
something like this:
import random
randoms=[]
while len(randoms)<5:

x=random.randin t(1,100)
if x in randoms:
continue
else:
randoms.append( x)

HTH. BTW it's a lot easier to read "10**10" or "1e10" instead of a big string of
zeros.

Peace

"james blair" <ta************ **@yahoo.com> wrote in message
news:a4******** *************** ***@posting.goo gle.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?
| Whats the best method to generate random numbers so that they are most
| likely unique??
| Thanks
Jul 18 '05 #2
ta************* *@yahoo.com (james blair) writes:
I am generating a random number using
random.randint( 1,10000000000)
At least in Python 2.2, 10000000000 is not a valid arg to randint, which
has to take two int (not long) values. 10000000000 won't fit in 32 bits.
I'm not sure about Python 2.3 or 2.4.
Whats the possibility that the numbers generated will be same when
generated by 100 users at the same time?
There's two levels where you have to think about that questions:

1) Is the underlying generator biased enough to make such a collision
more likely than pure chance? That would be a failure of what's
called the "poker test" (Knuth vol. 2) and Python's PRNG is supposed
to be good about that, so you shouldn't get extra (or too few)
collisions that way.

2) What's the likelihood of such a collision if the numbers from the
generator are really random? Let N be the number of distinct values,
in this case N= approx. 10**10. Let k be how many numbers you draw,
i.e. in this case k=100. Let the random numbers be a[1],a[2],...,a[N].
If there's no collision, it means that for every i,j with i,j in 1,2,...,k,
a[i] != a[j]. There are k**2/2 such (unordered) pairs, and for any i,j,
a[i]!=a[j] with probability 1-1/N. So the chance of all the pairs
being unequal is (1-1/N)**(k**2/2). That's about exp(-k**2/(2*N)),
or about 1 in 2 million for your parameters. The case N=365, k=23 is the
famous "birthday paradox". It says that if you have 23 people in a
room, there's a better-than-even chance that some two of them have the
same birthday. In general, if you have a set of random numbers with N
possible values, you can expect to see a collision after seeing
O(sqrt(N)) numbers. Those collisions are called "birthday collisions"
after the birthday paradox.
Whats the best method to generate random numbers so that they are most
likely unique??


If they're random, there is a chance that they will collide, and the
only way to make that less likely is to use a bigger range.
Jul 18 '05 #3
"james blair" wrote:
Whats the possibility that the numbers generated will be same when
generated by 100 users at the same time?
This has already been answered.
Whats the best method to generate random numbers so that they are most
likely unique??


Realy, there's no "most likely" way. You can either rely on the random
number generator (mersenne twister, which is pretty damn good) or you can
guarantee that they are unique by a couple of methods:

1. Keep track of which numbers have been generated, and if you get a repeat
re-generate it - works well if the total number of numbers to be generated
is small and the range is large;

2. Keep track of all the possible values, and as each one is chosen
eliminate it from the future possible values. Select values using
random.choice() ;

3. If you know the maximum number of random values you will ever generate,
and you want to guarantee no collisions, Python 2.3 has a superb way of
doing it ...
import random
random.sample(x range(100000000 0), 5) [463302274, 701637929, 319795767, 173458898, 500806835]

and you can then just hand out the values in order ...
import random
rand_values = iter(random.sam ple(xrange(1000 000000), 5))
rand_values.nex t() 724415074 rand_values.nex t() 316181550 rand_values.nex t() 107217351
rand_>>> rand_values.nex t()
828888162 rand_values.nex t() 599821642 rand_values.nex t() Traceback (most recent call last):
File "<stdin>", line 1, in ?
StopIteration

Note that there is one fewer zero in there than you want because ...
import random
random.sample(x range(100000000 00), 5)

Traceback (most recent call last):
File "<stdin>", line 1, in ?
OverflowError: long int too large to convert to int

Tim Delaney
Jul 18 '05 #4
ta************* *@yahoo.com (james blair) wrote in message news:<a4******* *************** ****@posting.go ogle.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."
alreadyUsedNumb ers = sets.Set()
while True:
randomNumber = random.randint( a, b)
if randomNumber not in alreadyUsedNumb ers:
alreadyUsedNumb ers.add(randomN umber)
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(sampleSpac e)
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
2494
by: Virus | last post by:
Ok well what I am trying to do is have 1.) the background color to change randomly with 5 different colors.(change on page load) 2,) 10 different quotes randomly fadeing in and out in random spots on the webpage. with a delay timer on them, so they keep changing as the page is open. Not random each time the page is loaded. If anyone can help it would be greatly appreaciated, I have tried many of
4
2695
by: Jack | last post by:
I have two files: sort_comparison.c++ my_sort.h sort_comparison.c++ calls this code in my_sort.h: void my_sort::fillArray(int arr,int n) { // const int random_number_range=1000000;
70
6233
by: Ben Pfaff | last post by:
One issue that comes up fairly often around here is the poor quality of the pseudo-random number generators supplied with many C implementations. As a result, we have to recommend things like using the high-order bits returned by rand() instead of the low-order bits, avoiding using rand() for anything that wants decently random numbers, not using rand() if you want more than approx. UINT_MAX total different sequences, and so on. So I...
10
5969
by: Johnny Snead | last post by:
Hey guys, Need help with this random sort algorithm private void cmdQuestion_Click(object sender, System.EventArgs e) { Random rnd = new Random(); //initialize rnd to new random object System.Random iRnd = new System.Random(); string theNum = iRnd.Next(0,8).ToString(); lblAnswer.Text = iRnd.Next(0,8).ToString();
5
3339
by: Peteroid | last post by:
I know how to use rand() to generate random POSITIVE-INTEGER numbers. But, I'd like to generate a random DOUBLE number in the range of 0.0 to 1.0 with resolution of a double (i.e., every possible double value in the range could come up with equal probability). I'd also like to be able to seed this generator (e.g., via the clock) so that the same sequence of random values don't come up every time. Anybody have an easy and fast...
8
3906
by: Daniel | last post by:
Hey guys Using Random(), how random is it, is it possible to be predicted? I have a requirement for a very good random number generator. I was doing something such as: Random randomSeed = new Random((int)_seedTimer.ElapsedTicks); _random = new Random(randomSeed.Next(0,99999999)); return random.Next(min, max);
4
10566
by: fatimahtaher | last post by:
Hi, I am supposed to create a program that generates a random number and then asks the user to guess the number (1-100). The program tells the user if he guessed too high or too low. If he guessed right, it asks the user is he/she wants to play again. If the answer is yes, it generates a random number and asks the user to guess the number again. The user can exit if he enters 0. I have created the following code so far but it does not work....
13
2802
by: Peter Oliphant | last post by:
I would like to be able to create a random number generator that produces evenly distributed random numbers up to given number. For example, I would like to pick a random number less than 100000, or between 0 and 99999 (inclusive). Further, the I want the range to be a variable. Concretely, I would like to create the following method: unsigned long Random( unsigned long num )
8
7553
by: Anil Gupte | last post by:
I had someone write a random number generator in C# (I am more of a VB programmer) and they came up with the following: public string GetRand(int count) { string number = ""; for (int i=0; i<count; i++) { Random Rnd = new Random(); number = number+Convert.ToString(Rnd.Next(0,9));
2
5472
by: alishaikhji | last post by:
I am working on a program which will need several different integer and float random numbers at different stages, for example: - At one point, I need a random number (float) in the range 0.1 to 10.0 - At one point, I need a random number (float) in the range 0.5 to 1.5 - At one point, I need a random number (float) in the range 0.3 to 3.0 - At one point, I need a random number (float) in the range 0.1 to 10.0 Also, I need to make it sure...
0
8341
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8754
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8542
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7362
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4343
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2760
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1984
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.