Random number between two numbers in IDL

Generating a random number, or an array of random numbers, between 0 and 1, in IDL is easy. Simply call upon RANDOMU. But how do you generate random numbers between two other numbers?

RANDOMU creates pseudo-random numbers between 0 and 1, and has a number of variables you can pass to it (but be careful when using it: Random Numbers Are…Uh, Not Random!). But what if you don’t want a random number between 0 and 1, and say between 2 and 5?

Easy.

All you need to do is multiply by the range and add the start! So for example for a random number between 2 and 5:

print,2+3*RANDOMU(seed,1)

Or say 10 random numbers between 4 and 10:

print,4+6*RANDOMU(seed,10)

Told you it was easy.

This entry was posted in IDL and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published.