我正在试图找到一个代码来从所有生成的抽奖中选择一个随机的获胜者,是否有一种更快的方法,Python可以更快地生成更多的抽签,如果是这样的话,生成数百万。到目前为止,这是我的密码。
import random
def genNumber(n = 4):
return "".join([str(random.randint(0,9)) for i in range(n)])
for i in range(12365):
word = random.choice(['blue', 'aqua', 'jade','plum', 'gold', 'navy', 'pink', 'grey', 'ruby', 'rose', 'teal',"lime",])
print("{} {} {}".format(word.title(), genNumber(4), genNumber(4)))
random.choice(word)发布于 2016-01-31 11:11:31
您当前的抽奖票生成算法存在一个问题:它可能会产生多次相同的票证。防止这种情况的一个简单方法就是按顺序生成票。如果您想要随机生成票,这也是可能的,但您需要跟踪所有产生的票,以防止重复。
下面的代码创建一个包含所有有效票证号码的列表,对该列表进行洗牌,然后将每个数字转换为一个颜色+数字字符串。我的代码只生成简单的数字字符串,但是如果您想要创建xxxx表单的数字,可以很容易地修复它。
首先,我将演示执行转换的函数。
colors = ['Blue', 'Aqua', 'Jade',]
num_colors = len(colors)
def num_to_ticket(n):
return '{0} {1}'.format(colors[n % num_colors], n // num_colors)
num_tickets = 15
for n in range(num_tickets):
print(num_to_ticket(n))输出
Blue 0
Aqua 0
Jade 0
Blue 1
Aqua 1
Jade 1
Blue 2
Aqua 2
Jade 2
Blue 3
Aqua 3
Jade 3
Blue 4
Aqua 4
Jade 4下面是对门票进行洗牌并随机选择一张的代码。此代码使用random.seed函数,因此结果是可重复的。如果您不调用random.seed,或者使用None参数(即random.seed(None))调用它,那么伪随机序列将被操作系统提供的随机数所播种,因此每次运行程序时,结果都会有所不同。
来自 module docs
random.seed(a=None, version=2)初始化随机数生成器。 如果省略a或None,则使用当前的系统时间。如果操作系统提供了随机源,则使用它们而不是系统时间(有关可用性的详细信息,请参阅os.urandom()函数)。
from random import seed, randrange, shuffle
colors = [
'Blue', 'Aqua', 'Jade',
#'Plum', 'Gold', 'Navy',
#'Pink', 'Grey', 'Ruby',
#'Rose', 'Teal', 'Lime',
]
num_colors = len(colors)
def num_to_ticket(n):
return '{0} {1}'.format(colors[n % num_colors], n // num_colors)
seed(1234)
num_tickets = 15
tickets = range(num_tickets)
shuffle(tickets)
for n in tickets:
print(num_to_ticket(n))
winner = randrange(num_tickets)
print('\nWinner:', num_to_ticket(winner))输出
Aqua 1
Jade 0
Jade 2
Blue 1
Aqua 2
Aqua 0
Blue 3
Blue 4
Aqua 4
Jade 1
Jade 3
Aqua 3
Blue 0
Blue 2
Jade 4
Winner: Blue 3如果使用Python 3,则需要更改这一行:
tickets = range(num_tickets)对此:
tickets = list(range(num_tickets))在我的2GB内存的旧的2 GHz机器上,我可以在大约30秒内洗牌10000000张票。
下面是一个修改后的版本,它以Jade 0038 0763的形式打印票证。它还允许您指定要生成的赢家数量。如果您希望每次数字都不同,只需每次向seed提供一个新的参数,或者干脆删除seed调用。
from __future__ import print_function
from random import seed, randrange, shuffle, sample
colors = [
'Blue', 'Aqua', 'Jade',
#'Plum', 'Gold', 'Navy',
#'Pink', 'Grey', 'Ruby',
#'Rose', 'Teal', 'Lime',
]
num_colors = len(colors)
def num_to_ticket(n):
color = colors[n % num_colors]
num = str(n // num_colors).zfill(8)
return '{0} {1} {2}'.format(color, num[:4], num[4:])
seed(1234)
num_tickets = 15
num_winners = 4
tickets = range(num_tickets)
shuffle(tickets)
for n in tickets:
print(num_to_ticket(n))
#Select winners
winners = sample(tickets, num_winners)
for i, num in enumerate(winners):
print('Winner #{0}: {1}'.format(i, num_to_ticket(num)))输出
Aqua 0000 0001
Jade 0000 0000
Jade 0000 0002
Blue 0000 0001
Aqua 0000 0002
Aqua 0000 0000
Blue 0000 0003
Blue 0000 0004
Aqua 0000 0004
Jade 0000 0001
Jade 0000 0003
Aqua 0000 0003
Blue 0000 0000
Blue 0000 0002
Jade 0000 0004
Winner #0: Jade 0000 0001
Winner #1: Jade 0000 0002
Winner #2: Blue 0000 0002
Winner #3: Jade 0000 0000下面是代码的“获胜者”部分的输出,当我将num_tickets更改为1500000时
Winner #0: Jade 0038 0763
Winner #1: Jade 0033 4760
Winner #2: Aqua 0034 9232
Winner #3: Jade 0046 6305在我的机器上大约需要5秒。
这里还有另一个更适合你的版本..。然而,它确实运行得更慢:在我的机器上生成1500,000张票(没有打印)大约需要12秒。
''' Generate random raffle tickets, and select winners
See http://stackoverflow.com/q/35113113/4014959
Written by PM 2Ring 2016.02.01
'''
from __future__ import print_function
from random import seed, randrange, shuffle, sample
colors = [
'Blue', 'Aqua', 'Jade',
'Plum', 'Gold', 'Navy',
'Pink', 'Grey', 'Ruby',
'Rose', 'Teal', 'Lime',
]
num_colors = len(colors)
def num_to_ticket(n):
color = colors[n % num_colors]
num = str(n // num_colors).zfill(8)
return '{0} {1} {2}'.format(color, num[:4], num[4:])
seed(1234)
num_tickets = 15
num_winners = 4
tickets = set()
while len(tickets) < num_tickets:
ticket = randrange(1200000000)
tickets.add(ticket)
for n in tickets:
print(num_to_ticket(n))
#Select winners
winners = sample(tickets, num_winners)
for i, num in enumerate(winners, 1):
print('Winner #{0}: {1}'.format(i, num_to_ticket(num)))输出
Pink 6158 1569
Blue 0074 9147
Pink 3460 8896
Pink 6232 8147
Ruby 9392 6899
Jade 9109 7596
Lime 4407 3259
Ruby 0839 3822
Aqua 6715 6348
Pink 9664 5353
Pink 2368 0977
Jade 0308 1402
Plum 7664 8093
Ruby 7887 7271
Plum 5822 2757
Winner #1: Pink 3460 8896
Winner #2: Plum 5822 2757
Winner #3: Blue 0074 9147
Winner #4: Pink 6158 1569https://stackoverflow.com/questions/35113113
复制相似问题