Tag: 随机

我怎样才能创build一个随机数在python密码保护?

我正在做一个Python项目,我想创build一个密码安全的随机数字,我该怎么做? 我已经在网上读到,规则随机数生成的数字不是encryption安全的,并且函数os.urandom(n)返回给我一个string,而不是一个数字。

从列表中随机select50个项目写入文件

到目前为止,我已经想出了如何导入文件,创build新文件,并随机化列表。 我无法从列表中随意select50个项目来写入文件? def randomizer(input,output1='random_1.txt',output2='random_2.txt',output3='random_3.txt',output4='random_total.txt'): #Input file query=open(input,'r').read().split() dir,file=os.path.split(input) temp1 = os.path.join(dir,output1) temp2 = os.path.join(dir,output2) temp3 = os.path.join(dir,output3) temp4 = os.path.join(dir,output4) out_file4=open(temp4,'w') random.shuffle(query) for item in query: out_file4.write(item+'\n') 所以如果总的随机文件是 example: random_total = ['9','2','3','1','5','6','8','7','0','4'] 我想要3个文件(out_file1 | 2 | 3),第一个随机集合为3,第二个随机集合为3,第三个随机集合为3(对于这个例子,但是我想创build的应该有50个) random_1 = ['9','2','3'] random_2 = ['1','5','6'] random_3 = ['8','7','0'] 所以最后的'4'将不会包括在内。 我如何从我随机select的列表中select50个? 更好的是,我怎样才能从原始列表中随机select50个?

在1到sys.maxsize范围内的随机数总是1 mod 2 ^ 10

我试图通过使用频率testing,运行testing和卡方检验来findPython(2.7.10)中可用的PRNG的统计属性。 为了进行频率testing,我需要将生成的随机数转换为二进制表示,然后计算1和0的分布。 我正在试验在python控制台上的随机数字的二进制表示,并观察这种奇怪的行为: >>> for n in random.sample(xrange(1, sys.maxsize), 50): … print '{0:b}'.format(n) … 101101110011011001110011110110101101101101111111101000000000001 110000101001001011101001110111111110011000101011100010000000001 110111101101110011100010001010000101011111110010001110000000001 100001111010011000101001000001000011001111100000001010000000001 1111000010010011111100111110110100100011110111010000000000001 111000001011101011101110100001001001000011011001110110000000001 1000100111011000111000101010000101010100110111000100000000001 11101001000001101111110101111011001000100011011011010000000001 110011010111101101011000110011011001110001111000001010000000001 110110110110111100011111110111011111101000011001100000000001 100010010000011101011100110101011110111100001100100000000000001 10111100011010011010001000101011001110010010000010010000000001 101011100110110001010110000101100000111111011101011000000000001 1111110010110010000111111000010001101011011010101110000000001 11100010101101110110101000101101011011111101101000010000000001 10011110110110010110011010000110010010111001111001010000000001 110110011100111010100111100100000100011101100001100000000000001 100110011001101011110011010101111101100010000111001010000000001 111000101101100111110010110110100110111001000101000000000000001 111111101000010111001011111100111100011101001011010000000001 11110001111100000111010010011111010101101110111001010000000001 100001100101101100010101111100111101111001101010101010000000001 11101010110011000001101110000000001111010001110111000000000001 100111000110111010001110110101001011100101111101010000000001 100001101100000011101101010101111111011010111110111110000000001 100010010011110110111111111000010001101100111001001100000000001 110011111110010011000110101010101001001010000100011010000000001 1111011010100001001101101000011100001011001110010100000000001 110110011101100101001100111010101111001011111101100000000000001 1010001110100101001001011111000111011100001100000110000000001 1000101110010011011000001011010110001000110100100100000000001 11111110011001011100111110110111000001000100100010000000000001 101111101010000101010111111111000001100101111001011110000000001 10010010111111111100000001010010101100111001100000000000001 111110000001110010001110111101110101010110001110000000000000001 100000101101000110101010010000101101000011111010001110000000001 101001011101100011001000011010010000000111110111100010000000001 10110101010000111010110111001111011000001111001100110000000001 10110111100100100011100101001100000000101110100100010000000001 10010111110001011101001110000111011010110100110111110000000001 111011110010110111011011101011001100001000111001010100000000001 […]

如何获得MySQL随机整数范围?

我试图生成一个随机的整数,我select1和60之间的每一行作为计时器。 SELECT downloads.date, products.*, (FLOOR(1 + RAND() * 60)) AS timer 我已经search并继续来到这个FLOOR函数,如何select一个范围内的随机整数。 这是给我一个每行1。 我错过了什么? 我在MySQL 5.0.75 inheritance人其余的查询我相信这可能是一个嵌套问题 SELECT * FROM ( SELECT downloads.date, products.*, FLOOR(1 + (RAND() * 60)) AS randomtimer, ( SELECT COUNT( * ) FROM distros WHERE distros.product_id = products.product_id ) AS distro_count, (SELECT COUNT(*) FROM downloads WHERE downloads.product_id = products.product_id) AS true_downloads […]

如何在C#中生成随机的颜色名称

我需要生成随机的颜色名称,例如“红色”,“白色”等。我该怎么办? 我能够产生这样的随机颜色: Random randonGen = new Random(); Color randomColor = Color.FromArgb(randonGen.Next(255), randonGen.Next(255), randonGen.Next(255)); 但我需要的名字,并不是像这样产生的所有颜色都有一个已知的名字。 谢谢

在MySQL中为现有数据生成GUID?

我刚刚导入了一堆数据到一个MySQL表,我有一个列“GUID”,我想基本上用新的和唯一的随机GUID填充所有现有的行。 我如何在MySQL中做到这一点? 我试过了 UPDATE db.tablename SET columnID = UUID() where columnID is not null 只是让每个领域都一样

随机数似乎不是随机的

我正在尝试生成6个字符或更less的随机base32数字。 这应该给大约10亿个不同的组合。 我创build了一个程序来生成这些“随机”数字。 不过,平均每4万代产生一个副本。 为什么这些“随机”的数字在十亿多种不同的组合中经常出现重复? 这是我的代码: static void Main(string[] args) { int seed = Environment.TickCount; Random r = new Random(seed); Dictionary<int, int> resultDictionary = new Dictionary<int, int>(); for (int x = 1; x <= 1000; x++) { Dictionary<string, int> dictionary = new Dictionary<string, int>(); try { while (true) { int rand = r.Next(0, 1073741823); CrockfordBase32Encoding […]

有和没有replace的加权随机select

最近,我需要从列表中加权随机select元素,无论是否有replace。 虽然有一些众所周知的好的algorithm用于未加权的select,有些用于加权select而没有replace(例如resevoiralgorithm的修改),但我找不到任何用于replace的加权select的好algorithm。 我也想避免使用藏库方法,因为我正在select一个很小的列表中的一小部分,这个列表足够小,可以放在内存中。 在这种情况下有没有人有最好的方法build议? 我有我自己的解决scheme,但我希望find更有效率,更简单,或两者兼而有之。

从List <E>中取n个随机元素?

我如何从一个ArrayList<E>获取n个随机元素? 理想情况下,我希望能够连续调用take()方法来获得另一个x元素,而不需要replace。

JavaScriptexpression式在每种情况下生成一个5位数字

对于我的seleniumtesting,我需要一个价值提供商在每一个情况下得到一个5位数的数字。 JavaScript的问题是Math.random的api只支持生成一个0.起始浮点数。 所以它必须在10000到99999之间。 所以如果它只会产生0.10000或者更高,那么会很容易,但是它也会产生0.01000 。 所以这种方法不成功: Math.floor(Math.random()*100000+1) 是否可以在每种情况下(在expression式中)生成一个5位数的数字?