wyvern.lib
Class Range

java.lang.Object
  extended bywyvern.lib.Range

public class Range
extends java.lang.Object

Encapsulates a Range [MIN..MAX] value, and can generate random numbers.

Version:
1.0, Jan 22, 1998
Author:
Steve Yegge

Field Summary
 int max
           
 int min
           
 
Constructor Summary
Range(int minimum, int maximum)
          Constructs a new Range.
 
Method Summary
static int computeDistance(int x1, int y1, int x2, int y2)
          Does a fast (table-based) lookup of the distance between two nearby points, as an integer.
static int computeDistance(java.util.List locs1, java.util.List locs2)
          Takes 2 location lists and computes the distance between the two closest points in the lists.
static int computeDistance(Point p1, Point p2)
          Does a fast (table-based) lookup of the distance between two nearby points, as an integer.
static boolean fiftyFifty()
          Synonym for randomBoolean().
static void main(java.lang.String[] argv)
          Tests the randomValue function.
static Range parseRange(java.lang.String value)
          Parses a range specifier into a min and max.
static int percent()
          Generates a random number from 1 to 100.
static boolean randomBoolean()
          Computes a 50/50 chance - a coin toss.
 int randomValue()
          Returns a random value in this range.
static int randomValue(int max)
          Returns a random value between zero and max.
static int randomValue(int min, int max)
          Returns a random int value in the specified range.
static int randomValue(Range r)
          Returns a random value in the specified range.
static int rollDice(int num, int diceSides)
          Rolls a number of dice of the specified number of size and returns the result.
static int rollDice(int num, int diceSides, int adjust)
          Rolls a number of dice of the specified number of size and returns the result.
static int rollDice(java.lang.String input)
           
 java.lang.String toString()
          Returns a String representation of this range (min..max).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

min

public int min

max

public int max
Constructor Detail

Range

public Range(int minimum,
             int maximum)
Constructs a new Range.

Parameters:
minimum - the minimum value of the range
maximum - the maximum value of the range
Method Detail

randomValue

public int randomValue()
Returns a random value in this range.

Returns:
a value chosen from min:max, inclusive

randomValue

public static int randomValue(Range r)
Returns a random value in the specified range.

Parameters:
r - a Range with min and max values set
Returns:
a value chosen from r.min:r.max, inclusive

randomValue

public static int randomValue(int min,
                              int max)
Returns a random int value in the specified range.

Parameters:
min - the min value (inclusive)
max - the max value (inclusive)

randomValue

public static int randomValue(int max)
Returns a random value between zero and max.

Parameters:
max - the max value (inclusive)

randomBoolean

public static boolean randomBoolean()
Computes a 50/50 chance - a coin toss.


percent

public static int percent()
Generates a random number from 1 to 100.


fiftyFifty

public static boolean fiftyFifty()
Synonym for randomBoolean().


parseRange

public static Range parseRange(java.lang.String value)
Parses a range specifier into a min and max.

Parameters:
value - the string containing the range. Has to be fairly strictly formatted: "min..max". Examples: "2..7", "0..100".
Returns:
a Range holding the parsed minimum and maximum values
Throws:
java.lang.IllegalArgumentException - if the value can't be parsed

rollDice

public static int rollDice(int num,
                           int diceSides)
Rolls a number of dice of the specified number of size and returns the result. Useful for generating numbers on a bell curve.

Parameters:
num - the number of dice to roll
diceSides - the number of sides on each die

rollDice

public static int rollDice(int num,
                           int diceSides,
                           int adjust)
Rolls a number of dice of the specified number of size and returns the result. Useful for generating numbers on a bell curve.

Parameters:
num - the number of dice to roll
diceSides - the number of sides on each die
adjust - the amount to add to the result after all the dice are rolled. This shifts the center of the bell curve; for example, to generate a bell curve around zero, you could pass 6 ten-sided dice (range: 6-60, average 33) with an adjust of -33.

rollDice

public static int rollDice(java.lang.String input)

computeDistance

public static int computeDistance(int x1,
                                  int y1,
                                  int x2,
                                  int y2)
Does a fast (table-based) lookup of the distance between two nearby points, as an integer. If the distance is too large, (larger than about 12), just returns maxint.

See performance notes in the overloaded version of computeDistance().

Parameters:
x1 - first point x
y1 - first point y
x2 - second point x
y2 - second point y
Returns:
distance

computeDistance

public static int computeDistance(Point p1,
                                  Point p2)
Does a fast (table-based) lookup of the distance between two nearby points, as an integer. If the distance is too large, (larger than about 12), just returns maxint.

NOTE: Subsequent profiling has shown that if you need to compute the distance between two points, it's much faster to inline the computation with the following code:

	int xdiff = x2 - x1;
	if ( xdiff < 0 ) xdiff = -xdiff;
	int ydiff = y2 - y1;
	if ( ydiff < 0 ) ydiff = -ydiff;
	double dist = Math.sqrt ( (xdiff << 2) + (ydiff << 2) );
 
If you need the integer distance, the last line can be:
	int dist = (int) Math.sqrt ( (xdiff << 2) + (ydiff << 2) );
 
The inlined calculation is over twice as fast, even though it's using floating point, probably because of the overhead of the method calls that Range.computeDistance() uses.

Bottom line is: avoid using this method unless you're lazy. (Realistically, they're both pretty fast, so it doesn't matter that much.)

Parameters:
p1 - first point
p2 - second point
Returns:
distance

computeDistance

public static int computeDistance(java.util.List locs1,
                                  java.util.List locs2)
Takes 2 location lists and computes the distance between the two closest points in the lists. Useful for computing the closest distance between two large GameObjects.

Parameters:
locs1 - first location list (containing Point objects)
locs2 - second location list (containing Point objects)
Returns:
the distance between the two closest points, if they are within about 12 squares, or Integer.MAX_VALUE/2

toString

public java.lang.String toString()
Returns a String representation of this range (min..max).


main

public static void main(java.lang.String[] argv)
Tests the randomValue function.