2

I know there are some similar questions,but all of them just return a list,I just need a number,each time random generate a unique single number:

Now I'm using Python to build a loop:

for _ in range(9):
        random_number=random.randint(1,11)
        print(random_number)

My question is how to make sure each time the random_number is unique,not repeating in all 9 times. I don't need a list,just the random_number

5
  • Check if the number has been used already and roll the random number again if it's not unique. Commented Apr 14, 2020 at 18:20
  • 2
    Generate a list of sequential numbers, shuffle it, then simply iterate it from start to end. Commented Apr 14, 2020 at 18:20
  • @PM77-1, post answers as answers, not as comments. Commented Apr 14, 2020 at 18:24
  • @Jim - I wanted to provide a guidance - not an answer. Commented Apr 14, 2020 at 18:26
  • It sounds like you may have found ways to do this, the insight may be to loop over the list of numbers, rather than trying to generate a new value independently each iteration. Commented Apr 14, 2020 at 18:27

4 Answers 4

6

You can use random.sample over the target range of numbers to pick a desired number of unique numbers from them:

import random
for random_number in random.sample(range(1, 12), 9):
    print(random_number)
Sign up to request clarification or add additional context in comments.

2 Comments

oo, TIL this exists!
Thank you,this is what I want!
4

If you want each number to be unique from all the others, you don't want a truly random number each time (it's in fact very improbable for random numbers within a small set to be unique). I'd do it like this:

numbers = list(range(1, 12))
random.shuffle(numbers)
for random_number in numbers[:9]:
    print(random_number)

Essentially it's like taking a deck of cards, shuffling it, and then dealing the top nine cards. You'll get nine "random" cards but they will never repeat since they're coming from the same deck and you aren't reshuffling in between deals (consequently they aren't truly random, which is why card-counting is a thing).

1 Comment

If anyone wants to investigate Sam's parenthetical comment there, look up the "Birthday Paradox".
2

random.sample is definitely the way to go, but another idea is to initialize all numbers in a list and then randomly pop one at a time:

import random

numbers = list(range(1, 12))
for _ in range(9):
    random_index = random.randint(0, len(numbers)-1)
    selected_rn = numbers.pop(random_index)

This has a nice interpretation: a predefined sequence of numbers that you point to a number of times and grab one number at random.

Comments

0

This produces a random number each time the codes are run, using numpy

import numpy as np
np.random.sample(1)[0]

Other applications can be added easily to any context. for example when range is defined:

import numpy as np
each_time_new_random_number = np.random.sample(1)[0]

start = 5 
stop = 16

start + (stop-start)*each_time_new_random_number

Good wind to your sails.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.