3

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

  • for small values of "random" ? – Mawg says reinstate Monica Nov 17 '11 at 02:59
  • 2
    possible duplicate of [Generating random results by weight in PHP?](http://stackoverflow.com/questions/445235/generating-random-results-by-weight-in-php) – Daniel A. White Nov 17 '11 at 03:01
  • sorry, Herman, but you just don't seem to have your requirements defined. Unless you can explain more explicitly than "a much higher to lower sequence probability" I fear that someone (not me) will close this question. Please tell us what tou want (what you really, reaaly want) and we will try to help. To me it sounds liek you want soem sort of "sliding scale" of probabliilty, but have not defined how it should slide. – Mawg says reinstate Monica Nov 17 '11 at 03:02
  • 2
    "much higher to lower sequence probability" must be the worst-presented question concerning an intricate mathematical problem. If you cannot express your requirements precisely even in human language, how can you expect to write a satisfactory computer program? – Kerrek SB Nov 17 '11 at 03:08
  • Sorry guys reworded.. English is not my native language :) – Hector Gomez Nov 17 '11 at 03:12

1 Answers1

14

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 
Webby
  • 2,655
  • 5
  • 26
  • 34
  • Good idea, works great for me too, just wondering if theres any easy solution to getting it less weighted for between 1 and 10? – James Andrew Jan 31 '13 at 11:35