Skip to main content
deleted 5 characters in body
Source Link
Thierry Lathuille
  • 24.4k
  • 10
  • 50
  • 57

We generate the first digit in the 1 - 9 range, then take the next 3 from the remaining digits:

import random

# We create a set of digits: {0, 1, .... 9}
digits = set(range(10))
# We generate a random integer, 1 <= first <= 9
first = random.randint(1, 9)
# We remove it from our set, then take a sample of
# 3 distinct elements from the remaining values
last_3 = random.sample(digits - {first}, 3)
print(str(first) + ''.join(map(str, last_3)))

The generated numbers are equiprobable, and we get a valid number in one step.

We generate the first digit in the 1 - 9 range, then take the next 3 from the remaining digits:

import random

digits = set(range(10))
first = random.randint(1, 9)
last_3 = random.sample(digits - {first}, 3)
print(str(first) + ''.join(map(str, last_3)))

The generated numbers are equiprobable, and we get a valid number in one step.

We generate the first digit in the 1 - 9 range, then take the next 3 from the remaining digits:

import random

# We create a set of digits: {0, 1, .... 9}
digits = set(range(10))
# We generate a random integer, 1 <= first <= 9
first = random.randint(1, 9)
# We remove it from our set, then take a sample of
# 3 distinct elements from the remaining values
last_3 = random.sample(digits - {first}, 3)
print(str(first) + ''.join(map(str, last_3)))

The generated numbers are equiprobable, and we get a valid number in one step.

deleted 5 characters in body
Source Link
Thierry Lathuille
  • 24.4k
  • 10
  • 50
  • 57

We generate the first digit in the 1 - 9 range, then take the next 3 from the remaining digits:

import random

digits = set(range(10))
first = random.randint(1, 9)
last_3 = random.sample(digits - set([first]){first}, 3)
print(str(first) + ''.join(map(str, last_3)))

The generated numbers are equiprobable, and we get a valid number in one step.

We generate the first digit in the 1 - 9 range, then take the next 3 from the remaining digits:

import random

digits = set(range(10))
first = random.randint(1, 9)
last_3 = random.sample(digits - set([first]), 3)
print(str(first) + ''.join(map(str, last_3)))

The generated numbers are equiprobable, and we get a valid number in one step.

We generate the first digit in the 1 - 9 range, then take the next 3 from the remaining digits:

import random

digits = set(range(10))
first = random.randint(1, 9)
last_3 = random.sample(digits - {first}, 3)
print(str(first) + ''.join(map(str, last_3)))

The generated numbers are equiprobable, and we get a valid number in one step.

added 82 characters in body
Source Link
Thierry Lathuille
  • 24.4k
  • 10
  • 50
  • 57

We generate the first digit in the 1 - 9 range, then take the next 3 from the remaining digits:

import random

digits = set(range(10))
first = random.randint(1, 9)
last_3 = random.sample(digits - set([first]), 3)
print(str(first) + ''.join(map(str, last_3)))

The generated numbers are equiprobable, and we get a valid number in one step.

We generate the first digit in the 1 - 9 range, then take the next 3 from the remaining digits:

import random

digits = set(range(10))
first = random.randint(1, 9)
last_3 = random.sample(digits - set([first]), 3)
print(str(first) + ''.join(map(str, last_3)))

We generate the first digit in the 1 - 9 range, then take the next 3 from the remaining digits:

import random

digits = set(range(10))
first = random.randint(1, 9)
last_3 = random.sample(digits - set([first]), 3)
print(str(first) + ''.join(map(str, last_3)))

The generated numbers are equiprobable, and we get a valid number in one step.

Source Link
Thierry Lathuille
  • 24.4k
  • 10
  • 50
  • 57
Loading