com.ccg.util
Class Random

java.lang.Object
  extended by com.ccg.util.Random

public final class Random
extends Object

Helper functions to help in the use of java.util.Random. When working on a simple WordSearch game for my kids, I found that there were a lot of handy little functions for making use of the java.util.Random class which were not provided elsewhere. This class provides some of these functions. There are functions for getting a random integer within a specified range, or shuffling (randomizing) the contents of a array, java.util.Vector or java.lang.String object. All of the members of this class are static, hence you typically would not create any objects of this type, but call the member functions directly instead. Source code to a com.ccg.example.util.Random example program which makes use of this class has been provided.

Version:
$Revision: 1.3 $
Author:
$Author: pkb $
See Also:
Random

Constructor Summary
Random()
           
 
Method Summary
static int getLessThan(int range, Random rng)
          Get a random integer value in the range of [0,N-1].
static int rollDice(int[] dice, int numSides, Random rng)
          Simulate rolling a number of N sided dice and get the results of each roll.
static int rollDice(int numDice, int numSides, Random rng)
          Simulate rolling a number of N sided dice.
static void shuffle(Object[] whatToShuffle, Random rng)
          Shuffles the order of the contents of an array.
static String shuffle(String whatToShuffle, Random rng)
          Shuffles the characters in a string.
static void shuffle(Vector whatToShuffle, Random rng)
          Shuffles the order of the contents of an java.util.Vector.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Random

public Random()
Method Detail

getLessThan

public static final int getLessThan(int range,
                                    Random rng)
Get a random integer value in the range of [0,N-1]. Sometimes it is useful to get a random value (particularily if you want to pick a random value from an array) which you know will be in the range of 0 or more, but less than some maximum number. This routine will gaurantee it. However, it only works (returns) positive numbers.

Parameters:
limit - Limit for the returned random number.
rng - Random number generator to generate the next integer value from - the nextInt() member will be used.
Returns:
Random number in the range of [0,limit-1]
See Also:
Random

shuffle

public static final String shuffle(String whatToShuffle,
                                   Random rng)
Shuffles the characters in a string. This routine goes through each character of the source string and computes a new random position in the result string for the character. The characters are then swapped. This should result in the contents of the string being shuffled.

Parameters:
whatToShuffle - Source string to be shuffled
rng - Random number generator to generate the next integer value from - the nextInt() member will be used.
Returns:
Shuffled string
See Also:
Random, getLessThan(int, java.util.Random)

shuffle

public static final void shuffle(Object[] whatToShuffle,
                                 Random rng)
Shuffles the order of the contents of an array. This routine goes through all the elements in an array of and computes a new random position for each Object. The Object's location in the array is then swapped with the object at the newly computed location. This should result in the contents of the array being shuffled (similar to a deck of cards).

Parameters:
whatToShuffle - Array of objects to shuffle.
rng - Random number generator to generate the random indexes for the results (using the nextInt() member) will be used.
See Also:
Random, getLessThan(int, java.util.Random)

shuffle

public static final void shuffle(Vector whatToShuffle,
                                 Random rng)
Shuffles the order of the contents of an java.util.Vector. This routine goes through all of the elements in an java.util.Vector and computes a new random position for each Object contained. The Object's location in the vector is then swapped with the Object at the newly computed location. This should result in the contents of the vector being shuffled (similar to a deck of cards).

Parameters:
whatToShuffle - Vector containing the objects to shuffle.
rng - Random number generator to generate the random indexes for the results (using the nextInt() member) will be used.
See Also:
Random, getLessThan(int, java.util.Random)

rollDice

public static final int rollDice(int numDice,
                                 int numSides,
                                 Random rng)
Simulate rolling a number of N sided dice. This routine will simulate rolling a number of N sided dice and will return the sum of the roll. Using this version of rollDice does not allow one to see what the values of the individual dice were, just the sum. It is assumed that the dice are numbered 1 to the number of sides (For example, if you roll 2 six sided dice the range of the values returned will be between 2 and 12).

Parameters:
numDice - How many dice to roll
numSides - How many sides on each dice
rng - Random number generator used to generate each roll.
Returns:
Sum of the dice rolled.
See Also:
rollDice(int[],int,java.util.Random), getLessThan(int, java.util.Random)

rollDice

public static final int rollDice(int[] dice,
                                 int numSides,
                                 Random rng)
Simulate rolling a number of N sided dice and get the results of each roll. This routine will simulate rolling a number of N sided dice and returns the sum of the roll. It also fills in the array with the value of each dice rolled. This allows one to see what the values of the individual dice were - if you don't care about the individual values of the 'dice', then use the other form of rollDice(). It is assumed that the dice are numbered 1 to the number of sides (For example, if you roll 2 six sided dice the range of the values returned will be between 2 and 12).

Parameters:
dice - Array to store the value of each die as it is rolled - the number of dice rolled will be determined by the length of the array (do not pass NULL).
numSides - How many sides on each dice
rng - Random number generator used to generate each roll.
Returns:
Sum of the dice rolled.
See Also:
Example Program, rollDice(int,int,java.util.Random), getLessThan(int, java.util.Random)


Copyright 1998-1998-2006 null. All Rights Reserved.