简体   繁体   English

随机选择一个数字,然后随机选择一个更大的数字python代码?

[英]Select a number randomly and then select a higher number randomly python code?

I am trying to build a psychological experiment for my PhD thesis. 我正在尝试为博士学位论文建立心理实验。 I am pretty new to python. 我对python很陌生。 What I am looking for is that I need to select a number from a list of numbers and then again select a number which should in one case higher than the previous one and in another case lower than the previous one. 我找的是我需要从号码列表中的号码 ,然后再次选择号码应在比前一个和在另一种情况下比前一较高下一个案件

The main list of pool of numbers is: 数字池的主要列表是:

digit = range(0,30)
if cong = 'yes':
## select a number for a variable d1 from digit and
## select another number for a variable d2 from digit which is higher than  
## d1
else:
## select a number for a variable d1 from digit and
## select another number for variable d2 which is lower than d1

I have tried solving this but I guess not that expert right now. 我已经尝试解决此问题,但我想现在还不是那个专家。 Would be grateful if anyone could help me solve this. 如果有人可以帮助我解决这个问题,将不胜感激。

PS: This is my first question so I am now much aware about the standard practice of forum participation. PS:这是我的第一个问题,因此我现在非常了解论坛参与的标准做法。

Thanks Vatsal 感谢瓦萨尔

LO, HI = 0, 30
d1 = random.randint(LO + 1, HI - 1) # Select a number between 0 and 30, exclusive
if cong == 'yes':
    d2 = random.randint(d1 + 1, HI) # Select a bigger number
else:    
    d2 = random.randint(LO, d1 - 1) # Select a smaller number

randint() and randrange() should do the job: randint()randrange()应该完成以下工作:

import random

num_range = a, b  #Specify the initial range here. (0, 30) for you.
num_0 = random.randrange(a+1, b)  #Boundaries aren't included.
if cong == "yes":
    num_1 = random.randint(num_0+1, b)  #Select a larger number.
else:
    num_1 = random.randint(a, num_0-1)  #Select a smaller number.

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

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