简体   繁体   English

随机数生成器,如何得到不一样的随机数

[英]Random number generator, how to get random numbers that are not the same

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. [1,2,3,4] 是完美的 - [1,1,2,4] 不是我想要的,因为一个数字重复出现。

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我是 python 的新手,我看到了几个像我这样的问题,但没有一个有同样的问题

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.我想生成一个随机数 = ANSWER 我想要生成第二个数字来尝试匹配 ANSWER,如果它不是 ANSWER,则将其存储在某处。 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')

Use random.sample() instead of populating the list one by one (see docs ):使用random.sample()而不是一一填充列表(参见文档):

Return ak length list of unique elements chosen from the population sequence.返回从种群序列中选择的唯一元素的 ak 长度列表。 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. random.sample()需要提供一个列表(或任何可迭代的,真的)以从中选择一个子集。 Here, I gave it a list of the first 10 numbers starting from 0.在这里,我给了它一个从 0 开始的前 10 个数字的列表。

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.我相信你做了不必要的复杂代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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