Masih
Masih

Reputation: 980

Generate random ranges

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?

Upvotes: 0

Views: 65

Answers (1)

Rodrigo
Rodrigo

Reputation: 100

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

Upvotes: 1

Related Questions