• 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: Jonathan Rochkind <email@hidden>
  • Date: Wed, 10 Mar 2004 11:07:11 -0600

At 2:46 PM +0100 3/10/04, David Griffith wrote:
Hi all,

Is there an easy way to generate a random int in the range  x <= int <= y?
I have found a couple of methods but they don't appear to do what I need.
I'm sure there's a simple answer....

There are probably a number of ways to do it, using standard Java API, or third party frameworks. Here's the method I use. Note that it's definitely not a cryptographically secure random number, it's just a reasonably pseudo-random number.


    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;
    }



Regards,
Dave.
_______________________________________________
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.
_______________________________________________
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:
    • Re: Random Int between x and y
      • From: Ray Ackland <email@hidden>
References: 
 >Random Int between x and y (From: David Griffith <email@hidden>)

  • Prev by Date: RE: Grrr! awakeFromInsertion...
  • Next by Date: Re: Whats the recommended way for cross-object information inquiry?
  • Previous by thread: Re: Random Int between x and y
  • Next by thread: Re: Random Int between x and y
  • Index(es):
    • Date
    • Thread