- All Known Implementing Classes:
Randomizer
public interface IRandomizer
Top-level abstraction of a utility to generate various random values.
To maximally decouple an application from the randomizer's implementation any variable representing a randomizer
should be typed to this interface, not to any concrete implementation. For example:
IRandomizer myRandomizer = Randomizer.Singleton;
- Author:
- swong
-
Method Summary
Modifier and TypeMethodDescriptionrandomBounds(Rectangle rect, Dimension maxDim)
Generates a randomly located and sized rectanglerandomChoice(Object x, Object y, double probX)
Returns a random choice of one of two objects, x and y, where probX is the probability that x will be picked (0<=x<=1).Generates a random colorReturns a random square Dimension, whose width is maxDim.width/2<=width<=maxDim.widthdouble
randomDouble(double min, double max)
Returns a random double greater than or equal to min and less than max.int
randomInt(int min, int max)
Returns a random integer greater than or equal to min and less than or equal to max.Generates a random location point subject to the constraint that 0<=X<=dim.width and 0<=Y<=dim.height.Generates a random location point subject to the constraint that rect.x<=X<=rect.x+rect.width and rect.y<=Y<=rect.y+rect.height.Returns a random velocity (as a Point) subject to the constraint that the absolute value of the velocity (speed) within the bound (inclusive) defined by rect.
-
Method Details
-
randomLoc
Generates a random location point subject to the constraint that rect.x<=X<=rect.x+rect.width and rect.y<=Y<=rect.y+rect.height. Note that the definition of a Rectangle object requires that (x,y) be defined as the upper left corner and that width and height must both be non-negative.- Parameters:
rect
- The bounds for the x and y values of the created Point- Returns:
- A Point object whose x and y are subject to the given bounds
-
randomLoc
Generates a random location point subject to the constraint that 0<=X<=dim.width and 0<=Y<=dim.height.- Parameters:
dim
- The bounds for the x and y values of the created Point- Returns:
- A Point object whose x and y are subject to the given bounds
-
randomInt
int randomInt(int min, int max)Returns a random integer greater than or equal to min and less than or equal to max.- Parameters:
min
- The minimum allowed valuemax
- The maximum allowed value- Returns:
- an int subject to the given bounds
-
randomDouble
double randomDouble(double min, double max)Returns a random double greater than or equal to min and less than max.- Parameters:
min
- The minimum allowed valuemax
- The maximum allowed value- Returns:
- a double subject to the given bounds
-
randomVel
Returns a random velocity (as a Point) subject to the constraint that the absolute value of the velocity (speed) within the bound (inclusive) defined by rect. Thus the resultant velocity may be negative. The given Rectangle should use all positive values. To create velocities or any Point with asymmetrical bounds, use the randomLoc(Rectangle rect) method with the input rectangle set to the desired bounds.- Parameters:
rect
- The bounds for the absolute value of the velocity in the x and y directions.- Returns:
- a Point object with x and y subject to the given bounds.
-
randomDim
Returns a random square Dimension, whose width is maxDim.width/2<=width<=maxDim.width- Parameters:
maxDim
- The bounds on the side of the created Dimension.- Returns:
- A random square Dimension subject to the given bound
-
randomBounds
Generates a randomly located and sized rectangle- Parameters:
rect
- The bounds for the location of the created rectanglemaxDim
- The bounds for the dimensions of the create rectangle- Returns:
- A Rectangle with location and dimensions subject to the given bounds.
-
randomColor
Color randomColor()Generates a random color- Returns:
- a random Color object
-
randomChoice
Returns a random choice of one of two objects, x and y, where probX is the probability that x will be picked (0<=x<=1).- Parameters:
x
- The first of two choicesy
- The second of two choicesprobX
- The probability of the first choice- Returns:
- Either x or y as per the probability of choosing them.
-