• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Random Int between x and y
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Random Int between x and y


  • Subject: Re: Random Int between x and y
  • From: Ray Ackland <email@hidden>
  • Date: Thu, 11 Mar 2004 13:44:33 +1300

Jonathan,

Actually, I don't know the answer (ie would have to look up the Java docs to find the correct functions to use) but basing the answer on David's code below, and assuming the return value of the function r.nextInt returns a value in the range (a, b) (the Java doc's telling you what that range is) and assuming y > x (in the two lines that followed in David's code, he allows for the case when it isn't), just change the line setting tempInt to be along the lines of:

tempInt = r.nextInt() / (float)(b - a) * (y - x) + x;

If, in fact, y < x, then the value you would get would still be a random number in the range (y, x), but it would be transformed down from x rather than up from it. Kind of irrelevant though.

You could then omit the two lines about checking whether x > y.

Please note: using the code as is would mean you don't ever hit the y value (with any realistic chance). So you would essentially have a random number from [x, y-1]. To fix this, replace the values of y with y + 1. Or put an extra line in there so the code would read:

z = y + 1;
tempInt = r.nextInt() / (float)(b - a) * (z - x) + x;
return tempInt;

Please note 2: I am not as careful and thorough with my coding here as the others tend to be, so haven't actually tested this (or even provided the whole method). I'm just working from my mind. So give it a test and don't be afraid of changing things in my suggestions that look wrong to you :)

Ray.

On 11/03/2004, at 12:48, Jonathan Rochkind wrote:

Thanks for the tip; I'm still a bit confused about how to do that. Do you have a suggestion?

I wouldn't use the % function to generate the random number. From my own experience (which I couldn't figure out until I heard it from someone else) the numbers generated that way are very NOT uniform.

That is, the least significant digits of the generated numbers aren't uniformly distributed. In fact, they are really badly proned to favour particular figures. Better to use a function that divides the range of the random number into the range of (x, y + 1).

Regards,

Ray.

On 11/03/2004, at 6:07, Jonathan Rochkind wrote:

At 2:46 PM +0100 3/10/04, David Griffith wrote:
...
    public static int randomIntBetween( int x, int y )
    {
      int tempInt, returnVal;
      java.util.Random r = new java.util.Random();
      tempInt = Math.abs( r.nextInt() % ( Math.abs( x - y ) + 1 ) );
      if ( x > y ) returnVal = tempInt + y;
      else returnVal = tempInt + x;
      return returnVal;
    }
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.


  • Follow-Ups:
    • Booleans
      • From: David Griffith <email@hidden>
References: 
 >Random Int between x and y (From: David Griffith <email@hidden>)
 >Re: Random Int between x and y (From: Jonathan Rochkind <email@hidden>)
 >Re: Random Int between x and y (From: Ray Ackland <email@hidden>)

  • Prev by Date: Re: Random Int between x and y
  • Next by Date: Booleans
  • Previous by thread: Re: Random Int between x and y
  • Next by thread: Booleans
  • Index(es):
    • Date
    • Thread