Jomar Sevillejo
Jomar Sevillejo

Reputation: 1698

Increasing chance of a certain number in Math.random within a range

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.

Upvotes: 0

Views: 348

Answers (1)

rikola
rikola

Reputation: 651

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.

Upvotes: 1

Related Questions