生成一个大的随机字符串 [英] Generating a large random string

查看:81
本文介绍了生成一个大的随机字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Aloha,

i想问另一个问题,但是当我开始建立一个例子......


如何生成(记忆和时间) - 一个包含

随机字符的字符串?我从未使用过发电机,所以我的解决方案

目前是:


导入字符串

导入随机
random.seed(14)

d = [random.choice(string.letters)for x in xrange(3000)]

s ="" .join(d)

打印s


这对3000来说是可行的,但我需要的长度在范围内

10.000到$ 1.000.


希望得到一个答案并希望有一个快乐的一天

LOBI

解决方案

你打算如何使用这个大的随机字符串?如果你只是计划打印,你可以写:

$ x $ b for x in xrange(100000):

sys。 stdout.write(random.choice(string.letters))


它会很快将大量垃圾转移到屏幕上。如果你想要用数据做其他事情,那么请具体说明你想要做什么。


Chris


" Andreas Lobinger" <一个************** @ netsurf.de>在消息中写道

news:40 *************** @ netsurf.de ...

Aloha,
我想问另一个问题,但是当我开始构建一个例子时...

如何生成(内存和时间) - 包含
随机字符的字符串?我从未使用过发电机,所以我的解决方案目前是:

导入字符串
随机导入
random.seed(14)
d = [random.choice(string.letters)for x in xrange(3000)]
s ="" .join(d)
print s

这是可行的对于3000,但我需要的长度在10.000到1.000.000之间。

希望得到一个答案并祝愿他们快乐的一天
LOBI


" Andreas Lobinger" <一个************** @ netsurf.de>在消息中写道

新闻:40 *************** @ netsurf.de ...

[snip]

如何生成(内存和时间) - 包含
随机字符的字符串?我从未使用过发电机,所以我的解决方案目前是:

导入字符串
随机导入
random.seed(14)
d = [random.choice(string.letters)for x in xrange(3000)]
s ="" .join(d)
print s

这是可行的对于3000,但我需要的长度在10.000到1.000.000之间。



[snip]


有有几件事要尝试,但这里有一次尝试'b $

相对较快,但其时间(和大小)仍然线性增长

,大小为n:

来自字符串导入字母

来自随机导入选项,样本,种子


#注意:应该使用timeit.py但是这样做...

从时间导入时钟到现在


n = 1000000


#你的方法

种子(14)

start = now()

s =''''。join([选择(字母)for x in xrange( n)])

take = now() - 开始

print" old way n:%d take:%2.2fs"%(n,take)


#不同的方法

seed(14)

#add 1 so population>样本大小(n)

factor = n / len(字母)+ 1

start = now()

s =''''。加入(样本(字母*因子,n))

take = now() - 开始

print" new way n:%d take:%2.2fs"% (n,take)

#输出:在Windows 98上测试500 + mhz 128MB

老路n:1000000花费:23.94s

new方式n:1000000花了:8.90s


有一个开始...


Sean

< br>

[snip]


#different approach
seed(14)
#add 1 so population>样本大小(n)
因子= n / len(字母)+ 1
start = now()
s =''''。join(样本(字母*因子,n))
take = now() - start
print" new way n:%d take:%2.2fs"%(n,take)



[snip] ]


这个方法出现了问题,因为它出现了

random.sample完成后无需替换,例如

来自随机导入样本
样本(范围(10),10)
[9,5,3,2,0] ,8,4,1,6,7]




很抱歉,

Sean


Aloha,
i wanted to ask another problem, but as i started to build an example...

How to generate (memory and time)-efficient a string containing
random characters? I have never worked with generators, so my solution
at the moment is:

import string
import random
random.seed(14)
d = [random.choice(string.letters) for x in xrange(3000)]
s = "".join(d)
print s

which is feasible for the 3000, but i need lengths in the range
10.000 to 1.000.000.

Hoping for an answer and wishing a happy day
LOBI

解决方案

How are you planning on using this large random string? If you are just
planning on printing, you can write:

for i in xrange(100000):
sys.stdout.write(random.choice(string.letters))

and it will dump lots of garbage to the screen pretty quickly. If you''re
looking to do something else with the data, be specific about what you''re
trying to do.

Chris

"Andreas Lobinger" <an**************@netsurf.de> wrote in message
news:40***************@netsurf.de...

Aloha,
i wanted to ask another problem, but as i started to build an example...

How to generate (memory and time)-efficient a string containing
random characters? I have never worked with generators, so my solution
at the moment is:

import string
import random
random.seed(14)
d = [random.choice(string.letters) for x in xrange(3000)]
s = "".join(d)
print s

which is feasible for the 3000, but i need lengths in the range
10.000 to 1.000.000.

Hoping for an answer and wishing a happy day
LOBI



"Andreas Lobinger" <an**************@netsurf.de> wrote in message
news:40***************@netsurf.de...
[snip]

How to generate (memory and time)-efficient a string containing
random characters? I have never worked with generators, so my solution
at the moment is:

import string
import random
random.seed(14)
d = [random.choice(string.letters) for x in xrange(3000)]
s = "".join(d)
print s

which is feasible for the 3000, but i need lengths in the range
10.000 to 1.000.000.


[snip]

There are several things to try, but here''s one attempt that''s
relatively fast but whose time (and size) still grow linearly
with the size of n:
from string import letters
from random import choice, sample, seed

# Note: should probably use timeit.py but this will do ...
from time import clock as now

n = 1000000

# your approach
seed(14)
start = now()
s = ''''.join([choice(letters) for i in xrange(n)])
took = now() - start
print "old way n: %d took: %2.2fs"%(n, took)

# different approach
seed(14)
# add 1 so population > sample size (n)
factor = n/len(letters) + 1
start = now()
s = ''''.join(sample(letters*factor, n))
took = now() - start
print "new way n: %d took: %2.2fs"%(n, took)
# Output: tested on Windows 98 500+mhz 128MB
old way n: 1000000 took: 23.94s
new way n: 1000000 took: 8.90s

There''s a start ...

Sean


[snip]


# different approach
seed(14)
# add 1 so population > sample size (n)
factor = n/len(letters) + 1
start = now()
s = ''''.join(sample(letters*factor, n))
took = now() - start
print "new way n: %d took: %2.2fs"%(n, took)


[snip]

There''s a problem with this method as it appears
random.sample is done without replacement, e.g.

from random import sample
sample(range(10), 10) [9, 5, 3, 2, 0, 8, 4, 1, 6, 7]



Sorry about that,
Sean


这篇关于生成一个大的随机字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆