Random number generation in MATLAB with condition

442 Views Asked by At

How can I generate random numbers in MATLAB with the condition that each new number has a minimum difference with all previously generated numbers for that matrix. So, X = rand(1,20) would generate 20 random numbers between 0 and 1, but how do I add the condition that each newly generated random number of those 20 has at least a difference of 0.05 with any previously generated number in that sequence of 20. Thanks

1

There are 1 best solutions below

1
On

The simple approach is to just generate new numbers one by one and check if it has the difference you want. If it does, then good. If it doesn't, discard it and try again.

This is a standard way of generating uniform random numbers on a non-standard domain. For instance, it is used in Java's Gaussian rng (based on the polar method) which starts with a random uniformly distributed point in the unit disc. Java constructs a random point in the square $[-1,1]\times[-1,1]$, and tries again and again until it is closer than $1$ to the origin.