Re: Math.random()



Lew wrote:
maya wrote:
oh brother... I think get gist of what you're saying.. will try it....;) thank you very much.. (boolean array.... hmmm.. not sure if I've ever even seen code for a boolean array...;)

package testit;
import java.util.Random;
import java.util.Set;
import java.util.HashSet;

public class Scrambler
{
private final Random rand = new Random();

public Set <Integer> scrambleEggs( int upper, int count )
{
Set <Integer> chosen = new HashSet <Integer> ( count * 4 / 3 + 1 );
boolean [] selected = new boolean [ upper ];

while ( count > 0 )
{
int indx = rand.nextInt( upper );
if ( ! selected [ indx ] )
{
if ( chosen.add( indx ))
{
--count;
}
selected [ indx ] = true;
}
}
return chosen;
}
}

Untried, untested. (Pseudo-)Non-deterministic run time.

The bizarre reference to chosen.add()'s return value is a hint that one doesn't really need the boolean array if one is using a Set, in that Set guarantees uniqueness of its values with respect to equals(), and add() tells you if the item was not already in the Set. This could really help if 'upper' has a large value.

Things get trickier if you want multi-threaded use or a deterministic number of times through the loop.


Why have the boolean array at all? The technique only makes sense if
upper is very large compared with count, so that the collision risk is
very low, and in that case it is bad to have to create an array with
upper elements. Here's a simplified, though still untested, version.

package testit;
import java.util.Random;
import java.util.Set;
import java.util.HashSet;

public class Scrambler
{
private final Random rand = new Random();

public Set <Integer> scrambleEggs( int upper, int count )
{
Set <Integer> chosen = new HashSet <Integer> ( count * 4 / 3 + 1 );

while ( chosen.size() < count )
{
chosen.add(Integer.valueOf(rand.nextInt( upper ));
}
return chosen;
}
}

Testing chosen.size(), rather than counting add attempts, automatically
deals with duplicates, because adding a duplicate does not increase the
size of the Set.

Patricia
.



Relevant Pages

  • Re: Math.random()
    ... public Set <Integer> scrambleEggs(int upper, ... The bizarre reference to chosen.add's return value is a hint that one doesn't really need the boolean array if one is using a Set, in that Set guarantees uniqueness of its values with respect to equals, and addtells you if the item was not already in the Set. ...
    (comp.lang.java.help)
  • Re: "Variable Depth" Problem
    ... number of issues with the code that I am tempted to discuss, but I'll refrain, and simply quote what he wrote: ... void print_r ... {int i; ... Enter n pairs (lower, upper): ...
    (comp.programming)
  • Re: Compare two numbers
    ... Then set the lower one as the lower limit,and the upper one as the upper ... int main ... The assignment operator assigns the right-hand side to the left-hand side, ... Uninitialized local variables start off ...
    (microsoft.public.vc.language)
  • Re: bytes calculation
    ... sizeof returns the number of chars in an object ... If the upper half of scnlen contains a value within the range of the upper ... int, bad things could happen, no? ...
    (comp.lang.c)
  • Re: String or string
    ... uses an upper case "S" and the other uses a lower case "s". ... appears in a light green color, while the lower case appears as blue. ... String and string mean the same thing but are in two different series of names. ... can be said for int but it has a few caveats, e.g. if you are working in a code base where you delve in and out of using 32 and 64 bit ints then it may ...
    (microsoft.public.dotnet.languages.csharp)