Random number generator, how to get random numbers

2019-03-07 04:59发布

问题:

I am making a random number generator but I do not want the numbers over again. so for example

[1,2,3,4] is perfect - [1,1,2,4] is not what I want because a number recurring.

I have looked on here and no one has the answer to the problem I am searching for, in my logic this should work but I do not know what I am doing wrong.

I am new to python and I saw a couple questions like mine but none with the same problem

import random, timeit
random_num = random.randint(1,10)
cont_randomized_num = random.randint(1,10)
tries = 0
start = timeit.default_timer()
num_guess = 0
stored_numbers = []

while cont_randomized_num != random_num:
    if cont_randomized_num == stored_numbers:
        cont_randomized_num = random.randint(1,10)
    elif cont_randomized_num != stored_numbers:
        print(num_guess)
        stored_numbers.append(cont_randomized_num)
        print(stored_numbers)
        cont_randomized_num = random.randint(1,10)
        tries +=1

print()
print()
stop = timeit.default_timer()
print(random_num)
print('Number of tries:',tries)
print('Time elapsed:',stop)
input('press ENTER to end')

I guess I did not make myself clear enough. I want to generate a random number = ANSWER I want a second number generated to try and match the ANSWER, if it is not the ANSWER then STORE it somewhere. If the second generated number is generated a second time and it is the same as the first time it was generated I want it to skip and generate a new one. Keep this going until the second generated number is equal to the first number generated.


I have figured it out (finally) here is the code that is not over complicated and have nothing to do with any answer or critique given! This is what I have been asking for this entire time.

import random
import timeit

start = timeit.default_timer()
stored_numbers = []
cont_random = random.randint(1,10)
random_num = random.randint(1,10)
times_guessed = 0

while random_num not in stored_numbers:
    if cont_random in stored_numbers:
        cont_random = random.randint(1, 10)
    elif cont_random not in stored_numbers:
        print(cont_random)
        stored_numbers.append(cont_random)
        cont_random = random.randint(1, 10)
        times_guessed += 1

print('Answer has been guessed!')
print('Stored numbers',stored_numbers)
print()
print()
stop = timeit.default_timer()
print('Time elapsed:',stop)
print('Times guessed -', times_guessed)
print('The random number:',random_num)
input('Press ENTER to exit')

回答1:

Here is my solution, you can add more functionality to it if you want by adding more code in the loop.

import random, timeit

def main():
    tries = 0
    NUMBER_OF_RANDOMS = 4
    stored_numbers = []
    start = timeit.default_timer()

    while len(stored_numbers) != NUMBER_OF_RANDOMS:
        current = random.randint(1,10)
        tries += 1
        if current not in stored_numbers:
            stored_numbers.append(current)

    stop = timeit.default_timer() - start
    print 'Number of tries:', tries
    print 'Time elapsed:', stop
    print stored_numbers

if __name__ == '__main__':
    main()

Does this do what you were wanting? I believe you do unnecessarily complicated code.



回答2:

Use random.sample() instead of populating the list one by one (see docs):

Return a k length list of unique elements chosen from the population sequence. Used for random sampling without replacement.

random.sample(range(10), 5)
>>>> [5, 9, 1, 6, 3]

random.sample() needs to be fed a list (or any iterable, really) to choose a subset from. Here, I gave it a list of the first 10 numbers starting from 0.



标签: python random