0

i want to produce random ranges with specific range of sizes between two numbers. first i thought there might be some specific functions in random module to do that , but it seems that there is not such function.

i wrote the following code but its slow.

import random
range_list=[]
n=0
while n<1000000:
     first=random.randint(1,3000000000)
     last=random.randint(1,3000000000)
     if abs(last-first) <150 and abs(last-first)>100:
         range_list.append([last,first])
     else:
         continue
     n+=1

is there any fast way to do this?

Masih
  • 920
  • 2
  • 19
  • 36
  • Produce two random numbers between 0 and 50, add (100 + a random number) to both. No? – 9000 Jul 07 '14 at 04:33
  • Adding random numbers together is not always a good idea. You could potentially ruin the "quality" of the randomness produced. Take a look at http://stackoverflow.com/a/3956538/525169, which explains this very well. That said, in this particular instance, since one random number has a very broad distribution and the other a very narrow one, the convolved distribution is likely to be quite flat as well - if that is what is intended. – Praveen Jul 07 '14 at 04:59

1 Answers1

1

I find it hard to do a fast program with values as high as the one you're using. But I would make it like this:

import random
range_list=[]

for n in range(1000000):
    first=random.randint(0,3000000000)
    last=random.randint(first+101,first+149)
    range_list.append([last,first])

Since you want (last-first)<150 and (last-first)>100 you can use last=random.randint(first+101,first+149) and then you won't need to use the "if" since the condition will already be true

Rodrigo
  • 100
  • 6