简体   繁体   English

我的随机整数仅返回4

[英]My random integer only returns 4

I'm trying to run this code: 我正在尝试运行以下代码:

import random
import time


random_int = random.randrange(1, 6)
time.sleep(.1)
for i in range(1, 101):
    print(i)
    print("Random: ", random_int)
    time.sleep(random_int)

But every time I run it, the random_int always returns the first number it chooses, so: 但是每次我运行它时,random_int总是返回它选择的第一个数字,因此:

1
Random:  4
2
Random:  4
3
Random:  4
4
Random:  4
5
Random:  4
6
Random:  4
7
Random:  4
8
Random:  4
9
Random:  4
10
Random:  4

Process finished with exit code 1 流程以退出代码1完成

Or: 要么:

1
Random:  2
2
Random:  2
3
Random:  2
4
Random:  2
5
Random:  2
6
Random:  2
7
Random:  2
8
Random:  2
9
Random:  2
10
Random:  2

Process finished with exit code 1

Etc. pls help won't let me submit this question without more text so this is text... how's life? 等帮助 下,如果没有更多文字,我将无法提交这个问题,所以这就是文字...生活如何? what ru doing? 你在做什么? don't answer... now wait! 不要回答...现在等待! answer my question but not these questions. 回答我的问题,但不能回答这些问题。

You need to call the random function inside the loop 您需要在循环内调用随机函数

import random
import time


time.sleep(.1)
for i in range(1, 101):
    print(i)
    random_int = random.randrange(1, 6)
    print("Random: ", random_int)
    time.sleep(random_int)

What you are doing is printing a variable random_int which has a value already assigned 您正在做的是打印一个变量random_int ,该变量已经分配了一个值

You have to "roll" your die: 您必须“滚动”自己的骰子:

import random
import time

time.sleep(.1)

for i in range(1, 101):
    print(i)
    random_int = random.randrange(1, 6)
    print("Random: ", random_int)
    time.sleep(random_int)

randrange generates a random, so, if you want to generate a new one each time, you have to call it. randrange生成一个随机数,因此,如果您想每次生成一个新的随机数,则必须调用它。

I'll answer it because I've been in desperation before 我会回答的,因为我以前很绝望
Going off of @user2357112: 从@ user2357112开始:
You are storing random_int = random.randrange(1, 6) once 您将一次存储random_int = random.randrange(1,6)
The loop is effectively doing nothing but referencing the random_int you stored first. 循环实际上除了引用您首先存储的random_int外什么也不做。
In order to have that change, you need to change the value in random_int, preferably by putting random_int = random.randrange(1, 6) inside the for loop, having it change every time. 为了进行更改,您需要更改random_int中的值,最好是将random_int = random.randrange(1,6)放入for循环中,使其每次都更改。
I hope that clears things up! 我希望这可以清除一切!

The problem is occuring as the assigning line(4th line) is outside the loop. 由于分配行(第4行)在循环外部,因此出现了问题。 You are basically assigning the random_int at first, then printing it again and again without changing the variable. 您基本上是先分配random_int,然后一次又一次地打印它而不更改变量。

import random
import time

random_int = random.randrange(1, 6)
time.sleep(.1)
for i in range(1, 101):
    print(i)
    print("Random: ", random_int)
    time.sleep(random_int)

In order to fix the problem, bring 'random_int = random.randrange(1, 6) ' inside the loop so it assigns the new value every single loops. 为了解决该问题,请将“ random_int = random.randrange(1,6)”放入循环中,以便在每个循环中分配新值。 Like this 像这样

import random
import time


time.sleep(.1)
for i in range(1, 101):
    random_int = random.randrange(1, 6)
    print(i)
    print("Random: ", random_int)
    time.sleep(random_int)

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

相关问题 我的函数返回一个包含单个整数的列表,如何让它只返回整数? - My function returns a list with a single integer in it, how can I make it return only the integer? 为什么我的随机整数变量不刷新? - Why is my random integer variable not refreshing? 绘制随机数仅返回整数 - Drawing random numbers returns only integers 任何整数的 Python 方法返回仅由数字 5 和 7 组成的列表 - Python method for any integer returns a list consisting of numbers 5 and 7 only 如何使 function 接收 integer 并仅返回其奇数位? - How to make a function that receives an integer and returns only its odd digits? 每次只返回一个数字的随机数生成器 - Random number generator that returns only one number each time 对于循环,在抓取时仅返回1和随机答案(python) - For looping returns only 1 and random answers while scraping (python) 编写一个 function 称为随机化,将正数 integer n 作为输入,并返回 A,一个随机的 nx 1 Numpy 数组 - Write a function called randomization that takes as input a positive integer n, and returns A, a random n x 1 Numpy array 我的神经网络在二进制分类中只返回 +1 - My neural network only returns +1 in binary classification 我的代码返回 JSONDecodeError 但文件中唯一的内容是 {} - My code returns a JSONDecodeError but the only thing in the file is {}
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM