Matlab function that generates random real numbers in a closed interval

2.7k Views Asked by At

It's there any function in Matlab that generates random real numbers in a closed interval. I found something with unifrnd() but it's generating numbers in an open interval. If I use unifrnd(x,y); I get (x,y) interval, instead of [x,y].

1

There are 1 best solutions below

0
On BEST ANSWER

Given the discussion of accuracy in the comments, you could use something like:

mag = floor(log10( y - x))  
num = unifrnd(x-(10^mag)*eps, y+(10^mag)*eps)

This essentially adds one "point" to the discrete interval representation, taking into account the accuracy based on the size of the numbers you're using. unifrnd() is essentially a wrapper around rand() (which means you don't really need the stats toolbox to do this), and thus it is really just scaling the uniform distribution on (0,1). If you're worried about the endpoints though, that matters, because you can't get more granular than the product the magnitude of your interval length with eps.