Re: can this code be improved



Print Guy wrote:
Hi all.

To most of you, what I have been working on is probably trivial and
easy, but it's taken me the good part of a week working 2 or 3 hrs a
day to finally figure out how to do what I needed to do.
Here in Canada, we have a lottery called 6-49. You pick 6 numbers
between 1 and 49. Those numbers are put on a ticket and there is a
draw twice a week.

I wanted to come up with a statistically solid way to pick my numbers
so I figured that if I were to pick 6 numbers 1,000,000 times and count
the number of times each number is selected, the top six would be good
numbers to bet on during the lottery.

You are assuming that any bias in the Java Random class also applies to
the lottery's random number generation method? I suspect it is using
genuinely random numbers, not just pseudo-random, and monitors them for
bias.


With that being said, I thought I could write a Java program to do the
job. It proved to be a bit more difficult than I thought it would be;
the hardest part being ensuring that each of the 6 numbers were unique.

Here is an alternative method for picking the 6 numbers.

There is a well-known linear time algorithm for shuffling an array, the
Fisher-Yates shuffle. On iteration i, the element with index i is
swapped with a randomly selected element between i and the end of the array.

There are two important loop invariants:

1. At the end of every iteration, the array contains a permutation
of its original contents.

2. After N iterations, the first N elements of the array are randomly
selected with equal probability.

In a normal shuffle, the number of iterations is equal to the size of
the array minus one, and all elements have been shuffled. (The last
iteration is not needed because it makes a random choice from one
element, and swaps that element with itself).

Why not run Fisher-Yates for 6 iterations, and then pick up the
first 6 elements of the array?

Patricia



/**
* Partially shuffle the elements of an int
* array
*
* On completion, data contains a permutation of
* its original contents, with the elements in
* the first count spaces selected randomly.
* There is no assurance of randomness for
* elements data[count] and later, and many of
* those elements may remain in their original
* positions.
*
* @param data
* The array
* @param count
* The number of elements to shuffle.
* If count is negative or greater than
* data.length, all elements are
* shuffled.
* @param rand
* partialShuffle gets its random
* numbers from this generator.
*/
public static void partialShuffle(int[] data, int count,
Random rand) {
if (count > data.length || count < 0) {
count = data.length;
}
for (int i = 0; i < count; i++) {
int pickIndex = rand.nextInt(data.length - i) + i;
int temp = data[pickIndex];
data[pickIndex] = data[i];
data[i] = temp;
}
}
.



Relevant Pages

  • Re: "how to split a string in a random way"
    ... I created an integer array and I pass it along with a ... number to Factorize(). ... This time I send array1 through the Shuffle() method just prior to sending ... Dim array3 As Integer= Utility.GenArray ...
    (microsoft.public.dotnet.languages.vb)
  • Re: round-robin an arraylist
    ... Of all of them the work on the Array is the fastest. ... RemoveAt(), Addparadigm to do the shuffle is only slightly slower than the ... public class RoundRobin { ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Readline using foreach and while
    ... condition is checked at the start of each iteration, if the array ... map doesn't recheck the count ... the passed list for each iteration. ... @ary, which comes from a scope outside this sub, now contains (h, a, b, ...
    (comp.lang.perl.misc)
  • Re: Memory Leak Problem
    ... And each iteration I generated about 700000 random numbers. ... The variable that contains these random numbers is a pointer, ... compiler which does not allow REAL valued array indexes. ... to use rout, which has a pointer attribute, as an argument and then ...
    (comp.lang.fortran)
  • Re: Card dealing and random repetition
    ... inspiring for an 'elegant' way to simulate a shuffle. ... The dealer divide the deck in two parts, array ... left-half-deck (here there is a slight probability of 2 card staying ...
    (comp.programming)