简体   繁体   中英

Python Sequential number generator with no repeats

I'm trying to create a number generator in a sort of unconventional way. I'm completely new to python. I came up with 2 approaches and I'm getting sort of the results that I want, but it's incomplete and I don't know where to go next.

Approach 1

col1 = [1,2,3,4,5,7,8,10,13,16] 
col2 = [2,4,5,6,7,9,11,13,14,15,17,18,19,20,21,22] 
col3 = [7,8,10,12,13,14,15,16,17,18,19,23,24,25,27,29,30] 
col4 = [9,12,17,18,19,20,22,23,24,25,26,28,30,31,33] 
col5 = [20,21,24,25,26,28,30,32,33,34,35] 
b1 = [1,3,5,7,8] 
b2 = [2,4,6,7,9,10,11,12] 
drum1 = random.sample(col1 + col2 + col3 + col4 + col5, k=5) 
drum2 = random.sample(b1 + b2, k=2) 
draw1 = random.sample(drum1, k=5) 
draw2 = random.sample(drum2, k=2) 
main_draw = draw1 + draw2 main_draw

[30, 13, 28, 5, 30, 2, 4]

Approach 2

for num in range(4): 
first = random.choice(col1) 
second = random.choice(col2) 
third = random.choice(col3) 
forth = random.choice(col4) 
fifth = random.choice(col5) 
bonus1 = random.choice(b1) 
bonus2 = random.choice(b2) 
draw = f'{first} {second} {third} {forth} {fifth} {bonus1} {bonus2}' print(draw)

7 6 7 23 21 5 2 
13 11 14 20 30 8 9 
4 6 23 17 20 5 11 
5 22 23 23 20 7 11 

In one approach I used the.choice and the other approach the.sample.

  1. My first goal is to get unique random numbers from the range of numbers I manually enter and not from say, random(1,30). I would like to give it the numbers I want it to rand between. But I want each number to be unique and as you can see from the results, that's not the case.
  2. My second goal is to develop this further so that from the numbers I provide in each column as you noticed, some of the numbers in different columns are the same, I want it that way, however, I would also like it to count up, so if it chose 7 from col1 then it should start from a number greater than 7 in col2 and if it is, say 10, then it should choose one of the numbers in col3 that is greater than 10 and so on until col5.

Can you please help me? Thank you

This does what you want. You're restricting the set of numbers for the later picks rather severely.

import random

col1 = [1,2,3,4,5,6,7,8,10,13,16]
col2 = [2,4,5,6,7,9,11,13,14,15,17,18,19,20,21,22] 
col3 = [7,8,10,12,13,14,15,16,17,18,19,23,24,25,27,29,30] 
col4 = [9,12,17,18,19,20,22,23,24,25,26,28,30,31,33] 
col5 = [20,21,24,25,26,28,30,32,33,34,35] 
b1 = [1,3,5,7,8] 
b2 = [2,4,6,7,9,10,11,12] 

def pickem():
    pick1 = random.choice(col1)
    pick2 = random.choice( [i for i in col2 if i > pick1] )
    pick3 = random.choice( [i for i in col3 if i > pick2] )
    pick4 = random.choice( [i for i in col4 if i > pick3] )
    pick5 = random.choice( [i for i in col5 if i > pick4] )

    bonus1 = random.choice(b1)
    bonus2 = random.choice(b2)

    return (pick1,pick2,pick3,pick4,pick5,bonus1,bonus2)

print( pickem() )
print( pickem() )
print( pickem() )
print( pickem() )
print( pickem() )
print( pickem() )

You could do this with a while clause:

final_list = []
print('Enter Number 1')   # Ask for num 1
number1 = input()
print('Enter Number 2')   # Ask for num 2
number2 = input()
while len(final_list) < 10:    # Continue running loop until your list has 10 digits, adjust to whatever you want 
    new_number = random.choice(range(number1, number2)   # Generate a new number
    if new_number in final_list:   # Check if already in list
        pass
    elif new_number <= final_list[-1]:    # Check if new num is greater most recent addition
        pass
    else:
        final_list.append(new_number)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM