Weighted Random Numbers

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
burzvingion
Forum Newbie
Posts: 11
Joined: Sun Apr 18, 2004 2:30 pm

Weighted Random Numbers

Post by burzvingion »

Hello im a noob to this forum but anywho...

Im making a random word generator for my made up language (aka Conlang) called Ceenera and have gotten to the point where i want the user to be able to choose a range of syllables for the resultant words, as well as the "nominal" number of syllables to use, so that its more likely that a word will be closer to the nominal number of syllables than the bounds. ie. the user inputs via a form:
min: 1 syllable
nom: 2 syllables
max: 5 syllables
num of words: 1000

the results might be something like:
101 words with 1 syllable
499 words with 2 syllables
210 words with 3 syllables
139 words with 4 syllables
51 words with 5 syllables

The problem is, rand() and mt_rand() allow you to specify a range, but everything in that range is equally probable to be chosen. If anyones ever made a script like this before it would be much appreciated!
viel dank!
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

yeah, there is such script in Code Snippets forum ;)

viewtopic.php?t=15769&highlight=
burzvingion
Forum Newbie
Posts: 11
Joined: Sun Apr 18, 2004 2:30 pm

ty

Post by burzvingion »

ok thanksmuch! i think that'll work
Post Reply