随机数生成器,如何获取不相同的随机数 [英] Random number generator, how to get random numbers that are not the same

查看:152
本文介绍了随机数生成器,如何获取不相同的随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个随机数生成器,但是我不想再次使用这些数字.例如

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

[1,2,3,4]是完美的- [1,1,2,4]不是我想要的,因为有一个数字重复出现.

[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.

我是python的新手,我看到了几个像我的问题,但没有一个遇到相同的问题

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')

我想我还不够清楚. 我想生成一个随机数= ANSWER 我想要生成第二个数字以尝试与ANSWER匹配,如果不是ANSWER,则将其存储在某个地方.如果第二次生成的数字是第二次生成的,并且与第一次生成的数字相同,我希望它跳过并生成一个新的数字.继续进行下去,直到第二个生成的数字等于第一个生成的数字.

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')

推荐答案

这是我的解决方案,如果需要,可以通过在循环中添加更多代码来为其添加更多功能.

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.

这篇关于随机数生成器,如何获取不相同的随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆