Author Topic: Generating a list of unique random numbers  (Read 33875 times)

Gr3yling

  • Rogueliker
  • ***
  • Posts: 168
  • Karma: +0/-0
    • View Profile
    • Email
Re: Generating a list of unique random numbers
« Reply #30 on: April 18, 2014, 02:20:17 AM »
This is what I was trying to do (I think), in pretty much the way I was trying to do it:


from random import randint


random_list = []

while len(random_list) <= 20:
    random_no = randint(0,20)
    Unique = True
    for entry in random_list:
        if random_no == entry:
            Unique = False
   
    if Unique == True:
        random_list.append(random_no)

print random_list

raw_input()


Yes, there are better ways to do it which have already been mentioned, but this one fits my weird criteria.  Sorry if someone already posted something essentially the same as this and I missed it.

Xecutor

  • 7DRL Reviewer
  • Rogueliker
  • *
  • Posts: 263
  • Karma: +0/-0
    • View Profile
Re: Generating a list of unique random numbers
« Reply #31 on: April 18, 2014, 04:00:52 AM »
Code: [Select]
import random
random_list = [0]*20
for i in range(0,20):
  k = random.randint(0,i)
  random_list[i]=random_list[k]
  random_list[k]=i
print random_list