• 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: Kieran Kelleher <email@hidden>
  • Date: Wed, 10 Mar 2004 10:54:07 -0500

This code works from command line. The formula you want is indicated below in the function.

Test it on command line after compiling using something like
> java RandIntBetweenXAndY 1000 1400



public class RandIntBetweenXAndY
  {
  public static void main(String[] args)
  		throws NumberFormatException
    {
		long result = 0L;
		long x = Long.parseLong(args[0], 10);
		long y = Long.parseLong(args[1], 10);


		System.out.println ( "x: " + x );
		System.out.println ( "y: " + y );

		// This is the formula you want here
		result = x + Math.round( Math.random() * ( y - x ) );

		System.out.println ( "Random Result: " + result );

    }
  }



-Kieran

PS. How's the market in Ireland for WO development? (I saw your ".ie" domain .... I'm originally from Co.Cork myself living in Florida for now)


________________________________________________________________
Config = OS X 10.3.2 / Java 1.4.1 / WO 5.2.2 / MySQL 4.0.18 / Connector-J 3.0.11
Blog: http://webobjects.webhop.org/



On Mar 10, 2004, at 8:46 AM, 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....


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.


References: 
 >Random Int between x and y (From: David Griffith <email@hidden>)

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