0

Question Summary

From this SO question I am aware of Math.random(). However, is there a way to increase the chance of a certain number in being chosen?

Situation

  1. I am on a job of creating a raffle system.
  2. I have 10 players.
  3. Each of them has 1 entry for the raffle.This means, initially, each of them has a chance of 1/10 of winning against the others.
  4. They can earn more entries for the raffle.

Problem I can assign a number for each player and do Math.random from 1 to 10, but say player 2 has earned another entry. How do I make number 2 get a better chance of winning?

Notes for possible duplicate claims I am aware of this question closely trying to do the same. However, I am in need of a pure Javascript solution (since this has to be in client side) using Math.random() or other functions you may suggest. Also, this is an entry based random and not a by percent(%) chance.

Community
  • 1
  • 1
Jomar Sevillejo
  • 1,648
  • 2
  • 21
  • 33
  • 3
    Put him as the 11th player? – PM 77-1 Feb 18 '16 at 00:56
  • 1
    Possible duplicate of [Generate A Weighted Random Number](http://stackoverflow.com/questions/8435183/generate-a-weighted-random-number) – Roger Ng Feb 18 '16 at 01:04
  • @RogerNg I think this is not a duplicate. The question you have provided is tagged javascript but is targeting groovy for Java and not client side javascript. (I need this to be in client-side javascript because it's gonna kill the server doing all the raffles.) – Jomar Sevillejo Feb 18 '16 at 01:16
  • 1
    @JomarSevillejo How about this one? http://stackoverflow.com/questions/12515994/weighted-random-number-generation-in-javascript – Roger Ng Feb 18 '16 at 01:24
  • 1
    @JomarSevillejo The code examples in the accepted answer to the question linked by Roger Ng are plain JavaScript. – Daniel Pratt Feb 18 '16 at 01:34
  • @DanielPratt Sorry, I missed to check the answer. I went through it again and indeed it is in javascript. However, the way it is weighing the chances is not by the "number of entries" basis, but by ratio or percent. I know "entry based" also has a corresponding ratio but I'd like to code it in a way where the concept of having "number of entries" is visible. – Jomar Sevillejo Feb 18 '16 at 01:40
  • @RogerNg That second question my friend is pretty close to what I want! and I am now beginning to have a few pictures in my head of how to do it. I can even start coding it right now, but for the sake of being helpful to SO, I still think this question needs an answer for future askers. The second link has somewhat already answered my question, but is not exactly the answer to the situation I have provided in the question, since it is in reverse weight, hence it cannot be a duplicate. – Jomar Sevillejo Feb 18 '16 at 01:44
  • I find Rikola's answer really close as well, but it does not contain an example which might not be very helpful to future askers in SO. Should I accept it as answer? What do you suggest? – Jomar Sevillejo Feb 18 '16 at 01:45

1 Answers1

1

Have each user assigned an array of ticket numbers stored in a property. When a user earns another ticket, just append it to the ticket numbers array property for them. This would generate the statistical result you want.

rikola
  • 641
  • 7
  • 21
  • Thanks for the answer Rikola, but can you show us an example of what you mean? It will also serve useful for the rest of the SO people :) – Jomar Sevillejo Feb 18 '16 at 01:06