Likelihood of pairs in a range?

  • Context: Undergrad 
  • Thread starter Thread starter derrend
  • Start date Start date
  • Tags Tags
    Likelihood Range
Click For Summary

Discussion Overview

The discussion revolves around the likelihood of randomly selecting matching pairs of numbers from a specified range using a computer script. Participants explore the implications of the observed decrease in the percentage of possibilities explored as the range increases, touching on concepts of probability and randomness in programming.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant notes that the percentage of possibilities explored decreases as the range increases, which they find counterintuitive.
  • Another participant explains that each pair selected has a 1/x probability of being a match, suggesting that the expected number of guesses to find a match is around x.
  • A different participant corrects the probability statement, asserting it should be 1/(x+1) due to the inclusive nature of the random selection method used.
  • Concerns are raised about the quality of the random number generation in Python, with suggestions to use a more secure method for experimentation.
  • One participant defends the quality of Python's random module, stating it employs a robust pseudo-random number generator.

Areas of Agreement / Disagreement

Participants express differing views on the probability of matching pairs, with some agreeing on the expected number of guesses while others contest the specific probabilities involved. The discussion remains unresolved regarding the implications of the observed results and the correctness of the probability statements.

Contextual Notes

There are unresolved assumptions regarding the behavior of the random number generator and its impact on the results, as well as the implications of the probability calculations presented.

derrend
Messages
1
Reaction score
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:
Physics news on Phys.org
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.
 
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"
 
Last edited by a moderator:
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().
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
Replies
1
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
1
Views
1K
Replies
1
Views
3K
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
3K