这是我到目前为止的代码:
from quick_pick import quick_pick
def main():
LIMIT = 67
number = 9
list_1 = []*number
quick_pick(number, LIMIT, list_1)
print (list_1)
main()
import random
def quick_pick(n,limit,lottery):
main_count = 0
while main_count <n:
lotto_numbers = random.randint(1, limit)
if lotto_numbers not in lottery:
lottery.append(lotto_numbers)
main_count += 1
return (lottery * n)
但是当我运行它时,我得到了这个:[21]
我不确定如何让所有9个数字显示在列表中,以便我可以打印它。如果有人可以提供帮助,我将不胜感激,因为这是我的部分任务,我需要它来完成其余部分。
答案 0 :(得分:2)
您可以使用random.sample
来选择数字:
limit = 67
n = 9
print(random.sample(range(1, limit + 1), n)) # [49, 32, 66, 57, 25, 9, 22, 4, 48]
答案 1 :(得分:0)
修复缩进,丢失* n
和中提琴(!),您的代码有效:
def quick_pick(n,limit,lottery):
main_count = 0
while main_count <n:
lotto_numbers = random.randint(1, limit)
if lotto_numbers not in lottery:
lottery.append(lotto_numbers)
main_count += 1
return lottery
>>> quick_pick(number,LIMIT,list_1)
[44, 43, 62, 13, 11, 25, 36, 29, 15]
正如您所看到的,问题是,在找到要添加到return
的第一个号码后lottery
,您才会n
。你需要等到它被填满。另外乘以Running tests
Test running started
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:310)
at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:279)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:253)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
at com.example.Login.LoginActivity.onCreate(LoginActivity.java:54)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.support.test.runner.MonitoringInstrumentation.callActivityOnCreate(MonitoringInstrumentation.java:534)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420)
at android.app.ActivityThread.access$900(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5294)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
(9)也没有多大意义。
答案 2 :(得分:0)
在某些特定分布之后获得随机数的另一种方法是直接使用概率分布..
示例:
import numpy as np
nums = np.random.uniform(-1, 1, (100, 2))
这将从基础均匀分布产生范围(-1,1)中的一百个二维点(范围内的所有数字具有相同的被拾取概率)。