我正在生成一个1到10范围内的数字,但想从该范围中排除数字2
,我不知道该怎么做。
这是我到目前为止所拥有的。
python文件
move = random.randint(1, 10)
因此,我想在1 to 10
之间生成数字,并排除数字2
。
答案 0 :(得分:4)
您可以使用random.choice
:
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<div class="container-fluid">
<div class="row">
<div class="my-5 py-4 offset-md-4 col-md-5 col-lg-4 justify-content-center align-items-center">
<form name="fos_user_registration_form" method="post" action="/app_dev.php/register/" class="fos_user_registration_register">
<div class="form-group">
<label for="fos_user_registration_form_name" class="required">Name</label>
<input type="text" id="fos_user_registration_form_name" name="fos_user_registration_form[name]" required="required" class="form-control" />
</div>
<div class="form-group">
<label for="fos_user_registration_form_line1" class="required">Address Line 1</label>
<input type="text" id="fos_user_registration_form_line1" name="fos_user_registration_form[line1]" required="required" class="form-control" />
</div>
<div class="form-group">
<label for="fos_user_registration_form_line2">Address Line 2 (optional)</label>
<input type="text" id="fos_user_registration_form_line2" name="fos_user_registration_form[line2]" class="form-control" />
</div>
<div class="form-row">
<div class="form-group col-md-4">
<label for="fos_user_registration_form_city" class="required">City</label>
<input type="text" id="fos_user_registration_form_city" class="w-100" name="fos_user_registration_form[city]" required="required" />
</div>
<div class="form-group col-md-4">
<label for="fos_user_registration_form_state" class="required">State</label>
<select id="fos_user_registration_form_state" class="w-100" name="fos_user_registration_form[state]"><option value="AL">Alabama</option><option value="AK">Alaska</option><option value="AZ">Arizona</option><option value="AR">Arkansas</option><option value="CA">California</option><option value="CO">Colorado</option><option value="CT">Connecticut</option><option value="DE">Delaware</option><option value="FL">Florida</option><option value="GA">Georgia</option><option value="HI">Hawaii</option><option value="ID">Idaho</option><option value="IL">Illinois</option><option value="IN">Indiana</option><option value="IA">Iowa</option><option value="KS">Kansas</option><option value="KY">Kentucky</option><option value="LA">Louisiana</option><option value="ME">Maine</option><option value="MD">Maryland</option><option value="MA">Massachusetts</option><option value="MI">Michigan</option><option value="MN">Minnesota</option><option value="MS">Mississippi</option><option value="MO">Missouri</option><option value="MT">Montana</option><option value="NE">Nebraska</option><option value="NV">Nevada</option><option value="NH">New Hampshire</option><option value="NJ">New Jersey</option><option value="NM">New Mexico</option><option value="NY">New York</option><option value="NC">North Carolina</option><option value="ND">North Dakota</option><option value="OH">Ohio</option><option value="OK">Oklahoma</option><option value="OR">Oregon</option><option value="PA">Pennsylvania</option><option value="RI">Rhode Island</option><option value="SC">South Carolina</option><option value="SD">South Dakota</option><option value="TN">Tennessee</option><option value="TX">Texas</option><option value="UT">Utah</option><option value="VT">Vermont</option><option value="VA">Virginia</option><option value="WA">Washington</option><option value="WV">West Virginia</option><option value="WI">Wisconsin</option><option value="WY">Wyoming</option></select>
</div>
<div class="form-group col-md-4">
<label for="fos_user_registration_form_zipcode" class="required">Zipcode</label>
<input class="w-100" type="text" id="fos_user_registration_form_zipcode" name="fos_user_registration_form[zipcode]" required="required" />
</div>
</div>
<div class="form-group">
<label for="fos_user_registration_form_phone">Phone (optional)</label>
<input type="text" id="fos_user_registration_form_phone" name="fos_user_registration_form[phone]" />
</div>
<small>We only ship within the US</small>
<div>
<button type="submit" id="register-submit-btn" class="btn btn-burnt-orange">Register account</button>
</div>
<input type="hidden" id="fos_user_registration_form__token" name="fos_user_registration_form[_token]" value="knrAUCbJNdBKVtkqR57qEzOAExZaqWmleKA4nRBiKeg" />
</form>
</div>
</div>
</div>
答案 1 :(得分:4)
您可以生成一个从1到9的随机值,如果大于或等于2,则将其移位1。
value = random.randint(1, 9)
move = value if value < 2 else value + 1
在数学上,您希望从9个元素中选择一个随机元素。您需要做的就是用2标识元素3,用3标识4,以此类推。很有可能,这就是我们所说的random variable。
随机变量定义为映射以下结果的函数 数量过程不可预测。
这种使用映射的策略在您的集合很大且生成代价很高时特别有用,但是映射规则相当简单。
改进:
U9-Forward指出,在这种情况下,可以使映射效率更高一些。将2映射到10就足够了。
value = random.randint(1, 9)
move = value if value != 2 else 10
答案 2 :(得分:1)
或者做另一种类似奥利维尔的答案的条件:
value = random.randint(1, 9)
move = 10 if move==2 else move
然后move
将不再是2
。
答案 3 :(得分:0)
一个显而易见且容易解释的路径是使用您提到的功能将qcc -Vgcc_ntoaarch64le -c -Wp,-MMD,build/aarch64le-debug/src/imrdls.d,-MT,build/aarch64le-debug/src/imrdls.o -o build/aarch64le-debug/src/imrdls.o -Wall -fmessage-length=0 -g -O0 -fno-builtin src/imrdls.s
qcc -Vgcc_ntoaarch64le -c -Wp,-MMD,build/aarch64le-debug/src/Test.d,-MT,build/aarch64le-debug/src/Test.o -o build/aarch64le-debug/src/Test.o -Wall -fmessage-length=0 -g -O0 -fno-builtin src/Test.c
qcc -Vgcc_ntoaarch64le -o build/aarch64le-debug/Test build/aarch64le-debug/src/Test.o build/aarch64le-debug/src/imrdls.o
排除在2
之外,并仅绘制数字,直到while-loop
被选中为止。这仍然效率很低,但是可以通过基本的编程概念来解决。
更直接的方法是更精确地定义列表,这意味着无需使用2
并使用2
函数:
random.choice
答案 4 :(得分:0)
您可以使用“ while”排除数字2
move = random.randint(1, 10)
while move == 2:
move = random.randint(1, 10)
此代码将生成随机数,直到不会为“ 2”