× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

How do you generate a random number between 0.xyz and 0.abc?

Ray Phan
Feb 23, 2015
<p>No that's pretty much the best way to generate a random number that is restricted to a certain range. Assuming that <code>0.abc &gt; 0.xyz</code>, you would simply do:</p> <pre><code>c = (n - m)*rand(N,1) + m; </code></pre> <p>First you would generate <code>N</code> random numbers between <code>[0, n - m]</code> which is what the first part of the above statement is doing, then with the addition of <code>m</code>, this would translate your range so that the values are <code>[m, n]</code>. For your case, <code>m = 0.xyz</code> and <code>n = 0.abc</code>.</p> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/28555408/How%20do%20you%20generate%20a%20random%20number%20between%200.xyz%20and%200.abc?/28555535">Stack Overflow</a>.</p>

Get New Tutorials Delivered to Your Inbox

New tutorials will be sent to your Inbox once a week.

comments powered by Disqus