<?xml version="1.0" encoding="UTF-8"?>
<code>
  <code>public static long randomNumber(int min, int max) {
	return Math.round((Math.random() * (max - min)) + min);
}</code>
  <comment>The title sais it: you'll get a random number within a given min and max.</comment>
  <created-at type="datetime">2007-10-01T00:50:20+00:00</created-at>
  <id type="integer">35</id>
  <language>Java</language>
  <permalink>get-a-random-number-within-a-given-interval</permalink>
  <refactors-count type="integer">10</refactors-count>
  <title>Get a random number within a given interval</title>
  <trackback-url></trackback-url>
  <updated-at type="datetime">2009-08-18T08:48:25+00:00</updated-at>
  <user-id type="integer">93</user-id>
  <refactors type="array">
    <refactor>
      <code>public static int randomNumber(int min, int max) {
	return min + (int)(Math.random() * (max - min));
}</code>
      <code-id type="integer">35</code-id>
      <comment>Why do you return a 'long', if the range is only specified by ints? The result always fits in an integer.
This is more or less the standard method for generating a random number. The only (usefull) change I can think of is:</comment>
      <created-at type="datetime">2007-10-01T05:35:36+00:00</created-at>
      <id type="integer">146</id>
      <language>Java</language>
      <rating type="integer">3</rating>
      <ratings-count type="integer">1</ratings-count>
      <title>On Get a random number within a given interval</title>
      <user-id type="integer">84</user-id>
      <user-name>typefreak</user-name>
      <user-website></user-website>
    </refactor>
    <refactor>
      <code>public static long randomNumber(int min, int max) {
	int number = new Random().nextInt(++max);		
	return number &lt; min ? number += min : number;
}</code>
      <code-id type="integer">35</code-id>
      <comment>Try to use MTRandom ("A very fast random number generator"):
http://www.koders.com/java/fid0498E2527646FD994A3DC7288DC13144CC0EA8FF.aspx
http://goui.net/doc/net/goui/util/MTRandom.html
http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
</comment>
      <created-at type="datetime">2007-10-01T10:55:06+00:00</created-at>
      <id type="integer">158</id>
      <language>Java</language>
      <rating type="integer">1</rating>
      <ratings-count type="integer">1</ratings-count>
      <title>On Get a random number within a given interval</title>
      <user-id type="integer" nil="true"></user-id>
      <user-name>Carlos Daniel de Mattos Mercer</user-name>
      <user-website>http://www.useinet.com.br</user-website>
    </refactor>
    <refactor>
      <code>private static int randomNumber(int min, int max) {
	int number = new Random().nextInt(++max);		
	return number &lt; min ? number += min : number;
}</code>
      <code-id type="integer">35</code-id>
      <comment></comment>
      <created-at type="datetime">2007-10-01T10:59:12+00:00</created-at>
      <id type="integer">159</id>
      <language>Java</language>
      <rating type="integer">1</rating>
      <ratings-count type="integer">1</ratings-count>
      <title>On Get a random number within a given interval</title>
      <user-id type="integer" nil="true"></user-id>
      <user-name>private int</user-name>
      <user-website></user-website>
    </refactor>
    <refactor>
      <code>public static int randomNumber(int min, int max) {
	int number = new Random().nextInt(++max);		
	return number &lt; min ? number += min : number;
}</code>
      <code-id type="integer">35</code-id>
      <comment></comment>
      <created-at type="datetime">2007-10-01T11:22:31+00:00</created-at>
      <id type="integer">161</id>
      <language>Java</language>
      <rating type="integer">1</rating>
      <ratings-count type="integer">1</ratings-count>
      <title>On Get a random number within a given interval</title>
      <user-id type="integer" nil="true"></user-id>
      <user-name>public</user-name>
      <user-website></user-website>
    </refactor>
    <refactor>
      <code>
public static int randomNumber(int min, int max) {
	int number = new Random().nextInt(++max);		
	return number &lt; min ? number += min : number;
}

</code>
      <code-id type="integer">35</code-id>
      <comment>This method will fail when the difference between min and max is smaller then a random number that is generated that is less then min.

(Min = 20, Max = 21, Number = 5)</comment>
      <created-at type="datetime">2007-10-01T11:59:34+00:00</created-at>
      <id type="integer">165</id>
      <language>Java</language>
      <rating type="integer">1</rating>
      <ratings-count type="integer">1</ratings-count>
      <title>On Get a random number within a given interval</title>
      <user-id type="integer" nil="true"></user-id>
      <user-name>Cameron Hatfield</user-name>
      <user-website></user-website>
    </refactor>
    <refactor>
      <code>public static int randomNumber(int min, int max) {
	return min + (new Random()).nextInt(max-min);
}</code>
      <code-id type="integer">35</code-id>
      <comment>The original implementation is awful. Passed min 0 and max 2, it has about a 25% chance of returning 0, 50% chance of returning 1, and 25% chance of returning 2. 

Passing through a double or doing rounding only introduces potential for blatant or subtle nonuniformity. Also, it would be wise to follow the underlying Java API's convention of being inclusive of the min value, exclusive of the max value.

The following does the right things. 

If this were a performance critical operation you might cache the Random instance in a static field. If you wanted reproducible 'random' series (as in simulations) you would cache a Random instance that was created with a client-contributed seed value. If you needed very high-quality (pseudo)randomness you would use SecureRandom instead, perhaps with a non-default algorithm/provider package. 

 



</comment>
      <created-at type="datetime">2007-10-01T16:00:39+00:00</created-at>
      <id type="integer">173</id>
      <language>Java</language>
      <rating type="integer">5</rating>
      <ratings-count type="integer">2</ratings-count>
      <title>On Get a random number within a given interval</title>
      <user-id type="integer" nil="true"></user-id>
      <user-name>Gordon Mohr</user-name>
      <user-website>http://gojomo.blogspot.com</user-website>
    </refactor>
    <refactor>
      <code></code>
      <code-id type="integer">35</code-id>
      <comment>Thanks for your infos Gordon - I'm not a very experienced Java programmer, so your "awful" is OK for me. Haven't tried my implementation with min=0 and max=2, so... thanks for your comment.</comment>
      <created-at type="datetime">2007-10-02T01:01:27+00:00</created-at>
      <id type="integer">190</id>
      <language>Java</language>
      <rating type="integer">0</rating>
      <ratings-count type="integer">0</ratings-count>
      <title>On Get a random number within a given interval</title>
      <user-id type="integer">93</user-id>
      <user-name>diskostu</user-name>
      <user-website nil="true"></user-website>
    </refactor>
    <refactor>
      <code></code>
      <code-id type="integer">35</code-id>
      <comment>You're welcome, thanks for your gracious response to my strong language. (Didn't mean to be harsh, just wanted to make a strong point.) 

FYI, the nonuniformity of the original code means the two endpoints always have half the chance of any internal number -- because the scaled-up double has to be within the first or last 0.5 range to be rounded to the endpoints, but can be within a 1.0 range to be rounded to an internal number. For example, with min=0 and max=3, 0 has a 1/6 chance, 1 and 2 a 1/3 chance each, and 3 a 1/6 chance.</comment>
      <created-at type="datetime">2007-10-02T13:34:20+00:00</created-at>
      <id type="integer">207</id>
      <language>Java</language>
      <rating type="integer">0</rating>
      <ratings-count type="integer">0</ratings-count>
      <title>On Get a random number within a given interval</title>
      <user-id type="integer" nil="true"></user-id>
      <user-name>Gordon Mohr</user-name>
      <user-website>http://gojomo.blogspot.com</user-website>
    </refactor>
    <refactor>
      <code>public static int myRandom (int min, int max) 
{
    return min + (int)Math.round((Math.random() * (max - min)));
}</code>
      <code-id type="integer">35</code-id>
      <comment>What Gordon posted will fail with for example inputs of 0 and 1. Mine wont.</comment>
      <created-at type="datetime">2009-01-18T00:53:16+00:00</created-at>
      <id type="integer">143794</id>
      <language>Java</language>
      <rating type="integer">0</rating>
      <ratings-count type="integer">0</ratings-count>
      <title>On Get a random number within a given interval</title>
      <user-id type="integer" nil="true"></user-id>
      <user-name>Ben</user-name>
      <user-website>-</user-website>
    </refactor>
    <refactor>
      <code></code>
      <code-id type="integer">35</code-id>
      <comment>Interesting,

I'm much easier with return min + (int)Math.round((Math.random() * (max - min)));
it is more easy to understand

Thanks for bringing this up</comment>
      <created-at type="datetime">2009-08-18T08:48:11+00:00</created-at>
      <id type="integer">262545</id>
      <language>Java</language>
      <rating type="integer">0</rating>
      <ratings-count type="integer">0</ratings-count>
      <title>On Get a random number within a given interval</title>
      <user-id type="integer" nil="true"></user-id>
      <user-name>software development london</user-name>
      <user-website>http://www.geeks.ltd.uk/Services.html</user-website>
    </refactor>
  </refactors>
</code>
