Men*_* A. 38 python random python-3.x
这工作得很好,但有时候数字从0开始:
import random
numbers = random.sample(range(10), 4)
print(''.join(map(str, numbers)))
Run Code Online (Sandbox Code Playgroud)
我发现了很多例子,但没有一个能保证序列不会以0.
Thi*_*lle 67
我们生成1到9范围内的第一个数字,然后从剩余数字中取出接下来的3个数字:
import random
# We create a set of digits: {0, 1, .... 9}
digits = set(range(10))
# We generate a random integer, 1 <= first <= 9
first = random.randint(1, 9)
# We remove it from our set, then take a sample of
# 3 distinct elements from the remaining values
last_3 = random.sample(digits - {first}, 3)
print(str(first) + ''.join(map(str, last_3)))
Run Code Online (Sandbox Code Playgroud)
生成的数字是等概率的,我们只需一步即可获得有效数字.
agh*_*ast 31
只需循环直到你有自己喜欢的东西:
import random
numbers = [0]
while numbers[0] == 0:
numbers = random.sample(range(10), 4)
print(''.join(map(str, numbers)))
Run Code Online (Sandbox Code Playgroud)
MSe*_*ert 20
这与其他答案非常相似,但不是sample或者shuffle你可以在1000-9999范围内绘制一个随机整数,直到得到一个只包含唯一数字的整数:
import random
val = 0 # initial value - so the while loop is entered.
while len(set(str(val))) != 4: # check if it's duplicate free
val = random.randint(1000, 9999)
print(val)
Run Code Online (Sandbox Code Playgroud)
正如@Claudio在评论中指出的那样,范围实际上只需要是1023 - 9876,因为该范围之外的值包含重复的数字.
一般来说random.randint会比快得多random.shuffle或random.choice因此即使它更可能一个需要借鉴多次(如@karakfa指出的),它比任何快达3倍shuffle,choice办法也需要join个位数.
tev*_*dar 15
我不太了解Python,但有点像
digits=[1,2,3,4,5,6,7,8,9] <- no zero
random.shuffle(digits)
first=digits[0] <- first digit, obviously will not be zero
digits[0]=0 <- used digit can not occur again, zero can
random.shuffle(digits)
lastthree=digits[0:3] <- last three digits, no repeats, can contain zero, thanks @Dubu
Run Code Online (Sandbox Code Playgroud)
一个更有用的迭代,实际创建一个数字:
digits=[1,2,3,4,5,6,7,8,9] # no zero
random.shuffle(digits)
val=digits[0] # value so far, not zero for sure
digits[0]=0 # used digit can not occur again, zero becomes a valid pick
random.shuffle(digits)
for i in range(0,3):
val=val*10+digits[i] # update value with further digits
print(val)
Run Code Online (Sandbox Code Playgroud)
在窃取其他解决方案中的碎片后,再加上来自@DavidHammen的小贴士:
val=random.randint(1,9)
digits=[1,2,3,4,5,6,7,8,9]
digits[val-1]=0
for i in random.sample(digits,3):
val=val*10+i
print(val)
Run Code Online (Sandbox Code Playgroud)
kar*_*kfa 11
拒绝抽样方法.从10位数创建一个4位数的随机组合,如果与标准不匹配则重新取样.
r4=0
while r4 < 1000:
r4=int(''.join(map(str,random.sample(range(10),4))))
Run Code Online (Sandbox Code Playgroud)
注意到这与@Austin Haskings的答案基本相同
Foo*_*167 11
[固定]在一个位置上移动所有四位数字不对.固定位置的交换前导零也不对.但是,前导零与九个位置中的任何一个的随机交换是正确的并给出相同的概率:
""" Solution: randomly shuffle all numbers. If 0 is on the 0th position,
randomly swap it with any of nine positions in the list.
Proof
Lets count probability for 0 to be in position 7. It is equal to probability 1/10
after shuffle, plus probability to be randomly swapped in the 7th position if
0 come to be on the 0th position: (1/10 * 1/9). In total: (1/10 + 1/10 * 1/9).
Lets count probability for 3 to be in position 7. It is equal to probability 1/10
after shuffle, minus probability to be randomly swapped in the 0th position (1/9)
if 0 come to be on the 0th position (1/10) and if 3 come to be on the 7th position
when 0 is on the 0th position (1/9). In total: (1/10 - 1/9 * 1/10 * 1/9).
Total probability of all numbers [0-9] in position 7 is:
9 * (1/10 - 1/9 * 1/10 * 1/9) + (1/10 + 1/10 * 1/9) = 1
Continue to prove in the same way that total probability is equal to
1 for all other positions.
End of proof. """
import random
l = [0,1,2,3,4,5,6,7,8,9]
random.shuffle(l)
if l[0] == 0:
pos = random.choice(range(1, len(l)))
l[0], l[pos] = l[pos], l[0]
print(''.join(map(str, l[0:4])))
Run Code Online (Sandbox Code Playgroud)
您可以使用3个数字的全范围,然后在剩余数字中选择前导数字:
import random
numbers = random.sample(range(0,10), 3)
first_number = random.choice(list(set(range(1,10))-set(numbers)))
print(''.join(map(str, [first_number]+numbers)))
Run Code Online (Sandbox Code Playgroud)
另一种方法,如果需要重复选择(如果你对数字位数保持合理),则是使用前导计算可能输出的列表itertools.permutations,过滤掉前导零的那些,并构建整数列表从中:
import itertools,random
l = [int(''.join(map(str,x))) for x in itertools.permutations(range(10),4) if x[0]]
Run Code Online (Sandbox Code Playgroud)
这是一些计算时间,但是之后你可以打电话:
random.choice(l)
Run Code Online (Sandbox Code Playgroud)
你想要多少次.它非常快,并提供均匀分布的随机性.
| 归档时间: |
|
| 查看次数: |
10149 次 |
| 最近记录: |