简体   繁体   English

在python中,是否每次都有一个变量是不同的随机数?

[英]In python, is there anyway to have a variable be a different random number everytime?

Basically I have this: 基本上我有这个:

import random
variable1 = random.randint(13, 19)

And basically what that does is assign variable1 a random number between 13 and 19. Great. 基本上,这样做是为variable1分配一个13到19之间的随机数。

But, what I want it to do is assign a different random number between 13 and 19 to that variable every time it is called. 但是,我要它做的是每次调用该变量时,在该变量之间分配一个13到19之间的不同随机数。 Is there anyway I can do this? 反正我能做到这一点吗?

If I'm not being clear enough here's an example: 如果我不够清楚,这里有一个例子:

import random
variable1 = random.randint(13, 19)
print(variable1)
print(variable1)
print(variable1)

And the output I want would look something like this: 我想要的输出看起来像这样:

./script.py
15
19
13

So yeah, anyway I could do this in python? 是的,无论如何我都可以在python中做到这一点? (More specifically python3. but the answer would probably be similar to a python2 answer) (更具体地说是python3。但答案可能类似于python2答案)

Perhaps a silly question, but why not just use a function instead of a variable? 也许这是一个愚蠢的问题,但是为什么不仅仅使用函数而不是变量呢?

Or perhaps more inline with your variable paradigm, why not use a method on an object. 也许更符合您的变量范例,为什么不对对象使用方法。 The object can then be passed as a variable, and the method returns the new random number. 然后可以将该对象作为变量传递,并且该方法返回新的随机数。

This is close to what you want 这接近您想要的

>>> import random
>>> variable1 = lambda: random.randint(13, 19)
>>> print(variable1())
18
>>> print(variable1())
15
>>> print(variable1())
17
>>> 

In that specific example, there's a rather asinine way of doing it. 在该特定示例中,有一种相当精明的方法。 print implicitly converts its argument into a string. print将其参数隐式转换为字符串。 You can overload that functionality: 您可以重载该功能:

class randomizer():
    def __str__(self):
        return str(random.randint(13, 19))

variable1 = randomizer()
print(variable1)
print(variable1)
print(variable1)

You mention the word "called", and in that way I think you have answered your own question. 您提到“被叫”一词,以这种方式,我认为您已经回答了自己的问题。 This is exactly the job of a function or method. 这正是功能或方法的工作。

A variable is just a reference to an object. 变量只是对对象的引用。 You could store an object in your variable which overrides the to string method in order to generate a random number, but this is still a method. 您可以将对象存储在变量中,该对象将覆盖to字符串方法以生成随机数,但这仍然是一个方法。

Ultimately what your asking for would be obfuscating what is really going on under the hood, and as a result would create less maintainable code, imho. 最终,您的要求将使幕后的实际情况变得晦涩难懂,结果将创建难以维护的代码,恕我直言。


When you do variable1 = random.randint(13, 19) , variable1 contains the immutable object "the number 13" (or some other number). 当您执行variable1 = random.randint(13, 19)variable1包含不可变对象“数字13”(或其他数字)。 By definition it cannot change without becoming another object. 根据定义,它不能更改而不会成为另一个对象。 Instead you could fill your variable with an aforementioned mutable object allowing it to modify its "random number". 取而代之的是,您可以使用前面提到的可变对象填充变量,以允许其修改其“随机数”。 Or you could just directly call a function. 或者,您可以直接调用一个函数。

Its simply a question of simplicity. 它只是一个简单的问题。

use: 采用:

alist = range(13,20)  
random.shuffle(alist)  

Now you can loop through this in random order 现在您可以按随机顺序遍历

How about using random.sample for your task? 在任务中使用random.sample怎么样? It Chooses k unique random elements from a population sequence . Chooses k unique random elements from a population sequence

import random
v1,v2,v3 = random.sample(range(13,20),3)
print v1,v2,v3

Of course, I have given this for 3 variables, you could use it for n variables. 当然,我给了3个变量,可以将其用于n个变量。

Why not skip the variable altogether? 为什么不完全跳过变量? You can use the function inline too: print(random.randint(13, 19) + 1) . 您也可以使用内联函数: print(random.randint(13, 19) + 1)

Try this, and see if it fits your needs ;) 试试这个,看看是否适合您的需求;)

print(random.randint(13, 19))
print(random.randint(13, 19))
print(random.randint(13, 19))
print(random.randint(13, 19))
print(random.randint(13, 19))

I can't see any other possibilities, so good luck! 我看不到其他任何可能性,祝您好运!

也许我没有正确理解问题,但您不能这样做:

print(random.randint(13, 19))

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

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