thenac
thenac

Reputation: 305

How to generate random numbers different from already generated ones?

I want to generate 18000 numbers (integers) between 36499 and 130064, but two times, and insert them in two different lists. I want the numbers in each list to be unique, that is, different from each other in the list, and different from every number in the other list. So I've decided to use the random.sample method and so far i have

import random
random.seed(9000)

a = random.sample(range(36499, 130064), 18000)

All good so far. However if I do the same for, say, a list called "b", some numbers will exist in list "a" as well. Is this any way you guys can think of to generate 18000 numbers different from the ones already generated? Thanks

Upvotes: 0

Views: 59

Answers (2)

JohanC
JohanC

Reputation: 80279

You could work with sets:

import random

random.seed(9000)
numbers = range(36499, 130064)
a = random.sample(numbers, 18000)
b = random.sample(set(numbers) - set(a), 18000)

Upvotes: 1

Kevin Wang
Kevin Wang

Reputation: 2729

you could generate 36000 random numbers instead of 18000, and then produce list A and list B from that.

Upvotes: 3

Related Questions