1

I have 6 different numbers, 11, 12, 13, 14, 15, 16 and I need to get one of them randomly. The point is, the number 11 has to be the most likely to be chosen, by far, like 90 or 95% more probable than the others.

I've resolved it like this:

numbersGroup =  [11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
                 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
                 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
                 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
                 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
                 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
                 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
                 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
                 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
                 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
                 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
                 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
                 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
                 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
                 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
                 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
                 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 13, 13, 14, 15, 15, 16]

selectedNumber = random.choice(numbersGroup)

It works, but I'm not really satisfied with this code. I think it's a bad solution and I have a feeling that there are better ways to resolve it. So, how would you guys do it?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Drumnbass
  • 867
  • 1
  • 14
  • 35
  • 1
    What you need is a [weighted choice](http://stackoverflow.com/a/3679747/2097780). – kirbyfan64sos Apr 15 '15 at 19:34
  • 1
    This is very similar to a question that was asked [here](http://stackoverflow.com/questions/3679694/a-weighted-version-of-random-choice), and there are two fairly decent answers. – Deacon Apr 15 '15 at 19:35
  • 1
    For this particular problem, I would do a two stage process. 1) see if the 90% option succeeds and return 11. 2) the 90% failed, so pick one from [12, 13, 14, 15, 16] or [12, 12, 13, 13, 14, 15, 15, 16] as per your array, and return the number picked. – rossum Apr 16 '15 at 23:26

0 Answers0