[Tutor] FW: (no subject)

Steven D'Aprano steve at pearwood.info
Fri Nov 30 11:03:10 CET 2012


Hello Tara, or is it Leon?

On 30/11/12 20:21, leon zaat wrote:

> Hi, im trying to write a script which randomly generates 10,000 points(x,y)
>in the unit square(so range between 0 and 1 for both x and y).so far I have
> written the code below in red,

What red? I see no red.

Please do not rely on colour in email, for at least two reasons:

1) For colour to be visible in your email, you must have so-called "Rich Text"
    (actually HTML mail) turned on. Many people have HTML mail turned off,
    because it is a security and privacy threat, or simply do not appreciate
    having somebody else's choice of colour and font being forced on them.

2) About 8% of men and 0.5% of women have red-green colour-blindness, with other
    forms of colour-blindness being less common. All up, about 1 in 10 men and 1
    in 100 women cannot easily or at all distinguish colours. Others may be
    partially or completely blind. Using a screen reader, they can still "read"
    the words you send, but will have no idea at all about colours.



>however it only produces one random point. How do I get it to repeat this so it
> produces 10,000 different random points?



import math
import random
random.seed()
x=random.uniform(0,1)
y=random.uniform(0,1)

This generates one random point, because it gets executed once. After generating that
single random point, you then loop 1x1 times:


for i in range(0,1):  # this line loops once
     for j in range(0,1):  # and this also loops once
         print (x,y)


so it only 1x1 = 1 time.

The solution is simple: if you want to generate 10,000 points, you must put the
code that chooses a random point inside something that will loop 10,000 times.
Here's an example that will loop 20 times:


for i in range(20):
     # Pick an x and y ar random
     x = random.uniform(0,1)
     y = random.uniform(0,1)
     print (x, y)




All you have to do now is extend that to loop 10000 times instead of 20.




-- 
Steven


More information about the Tutor mailing list