Likelihood of pairs in a range?

In summary, the conversation discussed running a computer script to test the time it takes to match two numbers selected at random within a given range. The percentage of possibilities explored before finding a correct answer decreased as the range increased, which seemed counter intuitive at first. However, each pair selected has a 1/(x+1) probability of being a match, and the expected number of guesses to get a match is around x+1. It was also mentioned that the default random number method in Python should not be used for security purposes. The Mersenne twister algorithm with a state size of 19937 bits is recommended for better randomness.
  • #1
derrend
1
0
Running a computer script (included below) I was testing to see how long it would take to match two numbers when selected at random from within a range. To my surprise the percentage of possibilities explored before finding a correct answer decreased as i raised the range.

Is this correct? It seems counter intuitive.--code--:
Code:
#!/usr/bin/python3

import sys
import random

x = int(sys.argv[1])
a = random.randint(0,x)
b = random.randint(0,x)

steps = 1
combos = x**2

while a != b:
	print('[{} {}]'.format(a,b), end=' ')
	a = random.randint(0,x)
	b = random.randint(0,x)
	steps += 1

percent = (steps / combos) * 100
print()
print()

print('[{} ! {}]'.format(a,b), end=' ')
print('equality!'.upper())
print('steps'.upper(), steps)
print('possble combinations = {}'.format(combos))
print('explored {}% possibilitys'.format(percent))
 
Last edited by a moderator:
Mathematics news on Phys.org
  • #2
Each pair selected has a 1/x probability of being a match. It does not matter what the first pair member is. The second pair member has 1/x probability of matching. The expected number of guesses to get a match is going to be around x.

You are looking at the ratio of x to x2. Of course this decreases as x increases.
 
  • #3
I'm not familiar with python, but if you want to do some experimentation with random numbers, you probably want to make sure first of all that you are using something better than the default random number method available in the language. Perhaps consider the following and see if that changes the result:

"Warning

The pseudo-random generators of this module should not be used for security purposes. Use os.urandom() or SystemRandom if you require a cryptographically secure pseudo-random number generator.

http://docs.python.org/2/library/random.html" [Broken]
 
Last edited by a moderator:
  • #4
jbriggs444 said:
Each pair selected has a 1/x probability of being a match.
It's 1/(x+1), not 1/x. The OP used randint(0,x), which returns a uniformly distributed random integer between 0 and x, inclusive.

The expected number of guesses to get a match is going to be around x.
x+1, to be precise.
hddd123456789 said:
I'm not familiar with python, but if you want to do some experimentation with random numbers, you probably want to make sure first of all that you are using something better than the default random number method available in the language.
Python's random module uses the Mersenne twister with a state size of 19937 bits. That is a very good pseudo random number generator. It is not the bad old rand().
 
  • #5


I would first analyze the code to ensure its accuracy and then conduct further experiments to confirm the results. However, based on the information provided, it does seem counterintuitive that the percentage of possibilities explored would decrease as the range increases. This could be due to random chance or other factors that were not accounted for in the experiment. It would be important to consider the limitations of the script and the potential for bias in the results. Further studies would be needed to fully understand the relationship between the range and the likelihood of pairs being found.
 

1. What is the likelihood of pairs occurring within a given range?

The likelihood of pairs occurring within a given range depends on the specific range and the type of pairs being considered. It is important to have a clear understanding of the probability distribution and any underlying factors that may affect the likelihood of pairs in a range.

2. How is the likelihood of pairs in a range calculated?

The likelihood of pairs in a range is typically calculated using statistical methods such as probability distributions and regression analysis. These methods take into account the range, sample size, and any relevant variables to determine the likelihood of pairs occurring within that range.

3. Is there a way to increase the likelihood of pairs in a range?

There are certain factors that may increase the likelihood of pairs in a range, such as increasing the sample size or controlling for variables that may impact the pairs. However, it is important to note that likelihood is not a guarantee and can only be estimated through statistical analysis.

4. Can the likelihood of pairs in a range be used to make predictions?

The likelihood of pairs in a range can be used to make predictions, but it is important to keep in mind that these predictions are based on probability and are not certain outcomes. Other factors may also influence the occurrence of pairs in a given range.

5. How can I interpret the likelihood of pairs in a range?

The interpretation of the likelihood of pairs in a range can vary depending on the specific context and data being analyzed. It is important to consider the range, sample size, and other relevant factors when interpreting the likelihood. It may also be helpful to consult a statistician or other expert in the field for a more accurate interpretation.

Similar threads

  • Programming and Computer Science
Replies
3
Views
977
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Set Theory, Logic, Probability, Statistics
Replies
2
Views
1K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
8
Views
758
  • Programming and Computer Science
Replies
8
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
6
Views
1K
  • Programming and Computer Science
Replies
9
Views
2K
Back
Top