views:

6169

answers:

8

Can anyone provide some pseudo code for a roulette selection function? How would I implement this: I don't really understand how to read this math notation.I want General algorithm to this.

+3  A: 

There are 2 steps to this: First create an array with all the values on the wheel. This can be a 2 dimensional array with colour as well as number, or you can choose to add 100 to red numbers.

Then simply generate a random number between 0 or 1 (depending on whether your language starts numbering array indexes from 0 or 1) and the last element in your array.

Most languages have built-in random number functions. In VB and VBScript the function is RND(). In Javascript it is Math.random()

Fetch the value from that position in the array and you have your random roulette number.

Final note: don't forget to seed your random number generator or you will get the same sequence of draws every time you run the program.

Bork Blatt
A: 

Random Number Generator pseudo code

  • add one to a sequential counter
  • get the current value of the sequential counter
  • add the counter value by the computer tick count or some other small interval timer value
  • optionally add addition numbers, like a number from an external piece of hardware like a plasma generator or some other type of somewhat random phenomena
  • divide the result by a very big prime number 359334085968622831041960188598043661065388726959079837 for example
  • get some digits from the far right of the decimal point of the result
  • use these digits as a random number

Use the random number digits to create random numbers between 1 and 38 (or 37 European) for roulette.

Mark Stock
I think random numbers have been solved in every programming language ever, and this process no longer has to be done by hand. Why didn't you mention wiring up all the half-adders while youre at it?
Karl
+3  A: 

Roulette selection function pseudo code:

  1. Spin wheel clockwise
  2. Launch ball around wheel counter-clockwise
  3. Await ball landing in partitioned and numbered space
  4. Observe and announce results

I may have the directions backwards. I’m not an expert in this particular technology. Sorry.

Jeffrey L Whitledge
:) +1 for honesty and humor.
Abyss Knight
"I may have the directions backwards"The wheel and the ball are always launched in opposite directions, but the directions are alternated. It's a major (some would say disproportionate) slap on the wrist for forgetting :) The ball must travel at least three times around before dropping or it's void
JoeBloggs
+1  A: 

Well, for an American Roulette wheel, you're going to need to generate a random integer between 1 and 38. There are 36 numbers, a 0, and a 00.

One of the big things to consider, though, is that in American roulette, their are many different bets that can be made. A single bet can cover 1, 2, 3, 4, 5, 6, two different 12s, or 18. You may wish to create a list of lists where each number has additional flages to simplify that, or do it all in the programming.

If I were implementing it in Python, I would just create a Tuple of 0, 00, and 1 through 36 and use random.choice() for each spin.

J.T. Hurley
+10  A: 

The other answers seem to be assuming that you are trying to implement a roulette game. I think that you are asking about roulette wheel selection in evolutionary algorithms.

Here is some Java code that implements roulette wheel selection.

Assume you have 10 items to choose from and you choose by generating a random number between 0 and 1. You divide the range 0 to 1 up into ten non-overlapping segments, each proportional to the fitness of one of the ten items. For example, this might look like this:

0 - 0.3 is item 1
0.3 - 0.4 is item 2
0.4 - 0.5 is item 3
0.5 - 0.57 is item 4
0.57 - 0.63 is item 5
0.63 - 0.68 is item 6
0.68 - 0.8 is item 7
0.8 - 0.85 is item 8
0.85 - 0.98 is item 9
0.98 - 1 is item 10

This is your roulette wheel. Your random number between 0 and 1 is your spin. If the random number is 0.46, then the chosen item is item 3. If it's 0.92, then it's item 9.

Dan Dyer
A: 

The expert advice for the long term should be, either don't play or start a gambling house. ;-).

Gamecat
A: 

Tirst, generate an array of the percentages you assigned, let's say p[1..n] and, assume total is the sum of all the percentage.

Then get a random number between 1 to total, let's say r

Now, the algorithm in lua:

   local  c  =  0
   for i = 1,n do
       c = c + p[i]
       if r <= c then
           return i
       end
   end
gray
A: 

This assumes some class "Classifier" which just has a String condition, String message, and double strength. Just follow the logic.

-- Paul

public static List<Classifier> rouletteSelection(int classifiers) {
    List<Classifier> classifierList = new LinkedList<Classifier>();
    double strengthSum = 0.0;
    double probabilitySum = 0.0;

    // add up the strengths of the map
    Set<String> keySet = ClassifierMap.CLASSIFIER_MAP.keySet();
    for (String key : keySet) {
        /* used for debug to make sure wheel is working.
        if (strengthSum == 0.0) {
        ClassifierMap.CLASSIFIER_MAP.get(key).setStrength(8000.0);
        }
         */
        Classifier classifier = ClassifierMap.CLASSIFIER_MAP.get(key);
        double strength = classifier.getStrength();
        strengthSum = strengthSum + strength;
    }
    System.out.println("strengthSum: " + strengthSum);

    // compute the total probability. this will be 1.00 or close to it.
    for (String key : keySet) {
        Classifier classifier = ClassifierMap.CLASSIFIER_MAP.get(key);
        double probability = (classifier.getStrength() / strengthSum);
        probabilitySum = probabilitySum + probability;
    }
    System.out.println("probabilitySum: " + probabilitySum);

    while (classifierList.size() < classifiers) {
        boolean winnerFound = false;
        double rouletteRandom = random.nextDouble();
        double rouletteSum = 0.0;

        for (String key : keySet) {
            Classifier classifier = ClassifierMap.CLASSIFIER_MAP.get(key);
            double probability = (classifier.getStrength() / strengthSum);
            rouletteSum = rouletteSum + probability;
            if (rouletteSum > rouletteRandom && (winnerFound == false)) {
                System.out.println("Winner found: " + probability);
                classifierList.add(classifier);
                winnerFound = true;
            }
        }
    }
    return classifierList;
}
Paul