Search code examples
php

PHP Weighted random number


How can I generate a weighted random number between 1 and 10 with 10 being the highest chance and 1 being the lowest chance?

rand(1,10) ?

Needs to be a simple one line code since it will be run 100,000's of times


Solution

  • OK I think I understand what you're trying to say..

    try this :

    mt_rand(mt_rand(1, 10),10 );
    

    I looped it a million times :

    10 = 292634 
    9 = 193333 
    8 = 142815 
    7 = 109580 
    6 = 84616 
    5 = 64498 
    4 = 47666 
    3 = 33450 
    2 = 21286 
    1 = 10122