简体   繁体   English

如何在python中生成8个不同的随机数?

[英]How do I generate 8 different random numbers in python?

I'm trying to write an encryption program that generates 8 different random numbers and converts them to ASCII. 我正在尝试编写一个加密程序,生成8个不同的随机数并将它们转换为ASCII。 If possible I'd like to use the 'random' function in python, but welcome other help. 如果可能的话我想在python中使用'random'函数,但欢迎其他帮助。

So far my code for generating the numbers is to assign a value to a different run of the random.randint() function 8 different times, the problem is that this is sloppy. 到目前为止,我生成数字的代码是将一个值分配给random.randint()函数的不同运行8次,问题是这是草率的。 A friend said to use random.sample(33, 126, 8) but I can't get this to work. 一位朋友说要使用random.sample(33, 126, 8)但我无法使用它。

Any help is extremely welcome. 非常欢迎任何帮助。

You can pass xrange with your upper and lower bound to sample: 您可以将带有上限和下限的xrange传递给sample:

from random import sample

print(sample(xrange(33, 126),8))

An example output: 示例输出:

[49, 107, 83, 44, 34, 84, 111, 69]

Or range for python3: 或者python3的range

 print(sample(range(33, 126),8))

Example output: 示例输出:

 [72, 70, 76, 85, 71, 116, 95, 96]

That will give you unique numbers. 这将给你独特的数字。

If you want 8 variables: 如果你想要8个变量:

a, b, c, d, e, f, g, h =  sample(range(33, 126), 8)

If you want ascii you can map(chr..) : 如果你想要ascii你可以map(chr..)

from random import sample

print map(chr,sample(xrange(33, 126), 8))

Example output: 示例输出:

['Z', 'i', 'v', '$', ')', 'V', 'h', 'q']

Using random.sample is technically not what you want - the values are not independent since after you choose the first number (from 93 options) you only have 92 options for the second number and so on. 使用random.sample在技​​术上并不是你想要的 - 这些值不是独立的,因为在你选择第一个数字(从93个选项)后,你只有92个选项用于第二个数字,依此类推。

If you're ok with that you can use Padraic's answer. 如果您对此感到满意,可以使用Padraic的答案。

If n (in your case n = 8 ) is much smaller than N (in your case N = 126-33 = 93 ) this should be fine, but the correct answer will be 如果n (在你的情况下n = 8 )远小于N (在你的情况下N = 126-33 = 93 )这应该没问题,但正确的答案将是

a, b, c, d, e, f, g, h = [random.randint(93, 126) for _ in xrange(8)]

edit: More importantly, if you decide to increase n to a state where n > N , you'll get a ValueError 编辑:更重要的是,如果你决定将n增加到n > N的状态,你将得到一个ValueError

If your goal is to pick 8 random ASCII characters in the range [33..126], you can do that directly. 如果您的目标是在[33..126]范围内选择8个随机ASCII字符,则可以直接执行此操作。 First, ASCII characters in that range is called string.printable . 首先,该范围内的ASCII字符称为string.printable You can use the random.sample function to pick out 8 from there: 你可以使用random.sample函数从那里挑出8:

import string
import random

result = random.sample(string.printable, 8)

result is now a list of 8 random printable characters. result现在是8个随机可打印字符的列表。

I recommend using a generator. 我建议使用发电机。 It will only work if you use python 2.5 or above. 只有使用python 2.5或更高版本才能使用它。

from random import randint

def randnums(number, startnum=0, endnum=10):
    for i in range(1, number + 1):
        yield randint(startnum, endnum)

print list(randnums(8, endnum=100))

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

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