开发者

Can python output multiple random numbers? How can they be uniformly distributed and also be random?

开发者 https://www.devze.com 2023-02-16 10:46 出处:网络
I have started to learn python. I am using the book \"Python scripting for compu开发者_JS百科tational science.\" I am trying to write a program for

I have started to learn python. I am using the book "Python scripting for compu开发者_JS百科tational science." I am trying to write a program for

"Write a script that prints a uniformly distributed random number between
−1 and 1. The number should be written with four decimals as implied by
the %.4f format."

I am using random.uniform to get the random number. But I can't find a function to get more than one random number. For example my code is

print random.uniform(-1, 1)

It gives the output 0.238638833859. But I want more than one uniform random number. Can any body tell me a function? One more thing, I want to learn python for scripting purpose. So can anybody suggest a good book? I want to use python for web automation. The book I am using is not ideal for that. Kindly suggest me good book.


Could you be looking for something like this:

[random.uniform(-1+x, 1+x) for x in range(10)]


Try that:

for x in range(4):
    print '%.4f' % random.uniform(1,-1)

Maybe you'll enjoy the book:

Learning Python - 4th Edition by Mark Lutz

0

精彩评论

暂无评论...
验证码 换一张
取 消