1
$\begingroup$

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

$\endgroup$
1
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ Thanks. Could you give me some help with the code for that, as I am not sure how to then keep the right amount of good numbers as the final sequence. I need to make a Monte Carlo simulation, running a sequence 1000 times, and these random number sequences are part of that. $\endgroup$ – Nikita Nov 8 '14 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.