Roulette Wheel Selection Algorithm in MATLAB

I’m not a MATLAB expert myself, but I had to code the roulette wheel selection algorithm, once again, this time in the MATLAB programming language. It complements this post, so I thought I could share it:

% ---------------------------------------------------------
% Roulette Wheel Selection Algorithm. A set of weights
% represents the probability of selection of each
% individual in a group of choices. It returns the index
% of the chosen individual.
% Usage example:
% fortune_wheel ([1 5 3 15 8 1])
%    most probable result is 4 (weights 15)
% ---------------------------------------------------------
function choice = fortune_wheel(weights)
  accumulation = cumsum(weights);
  p = rand() * accumulation(end);
  chosen_index = -1;
  for index = 1 : length(accumulation)
    if (accumulation(index) > p)
      chosen_index = index;
  choice = chosen_index;

It can be probably rewritten in a nice MATLAB way, but it’s useful as is.

12 thoughts on “Roulette Wheel Selection Algorithm in MATLAB

  1. vahid August 1, 2011 / 1:35 am

    thank you
    this is useful

  2. Carlos January 26, 2012 / 1:41 pm

    Great Man!
    Thank you

  3. Ekasteel February 15, 2012 / 3:17 pm

    try this: instead of the for loop

    index = find(accumulation>p,1)

    It will give u the index for the first term in accumulation that is larger than p without a loop.

    • LuisAnton February 18, 2012 / 3:51 pm

      Thanks! I’m not using Matlab anymore, but someone may find that useful.

  4. ajayram September 5, 2012 / 3:40 pm

    Could you please explain how the most chosen element is 4th one ?
    Because in this line you have done
    p = rand() * accumulation(end);
    So this means taking a uniformly distributed random number between 0 and 1 and multiplying it by the sum of all weights (accumulation(end))
    and then you pick up the first index greater than this value in the accumulation array

    How does this ensure that the most chosen element is the 4th one or the most chosen weight is 15 ???

    • LuisAnton September 6, 2012 / 11:22 am

      Well, that’s precisely the idea behind this algorithm! Have a look at the original post, here, where there’s a graphic explaining that.

      I’m not picking the first index greater than that value, but the index of the first bin the accumulation array which value is greater than that random number!

  5. mohamad January 18, 2013 / 2:29 pm

    thank you

  6. Ben Taylor March 27, 2014 / 11:04 pm

    Thanks, the Matlab way is to eliminate all looping, you can do that by just taking the absolute minimum and pulling out the index that way.

    For example:
    accumulation = cumsum(Fitness-min(Fitness));
    [ma indivudual_picked]=min(abs(accumulation-rand * accumulation(end)));

    indivudual_picked is what you want, also if you have negatives in your fitness they can screw this up so I subtract out the min.

    • LuisAnton March 28, 2014 / 9:23 am

      Woa, I’ve not worked with Matlab for a few years, so I’ll believe you 😀

  7. Kris March 29, 2014 / 2:25 am

    Thanks a lot!

  8. ANITA January 11, 2016 / 9:05 am

    can any variable in the algorithm be changed to get better selection? Can you suggest any other selection algorithm?

    • LuisAnton January 13, 2016 / 10:10 am

      Increasing the probability of a given slot of being selected is just a matter of setting a proper weight. I guess it depends on what’s a ‘better selection’. What do you mean?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.