DamienRoche Posted September 27, 2008 Share Posted September 27, 2008 Just to clarify. I'm looking for the best way in which to perform and action (call a variable) determined by an exact percentage. I did do a little rand(); test and came up with this. $name1 = "Damien"; $name2 = "Liam"; $name3 = "Deiter"; $n1weight = "25"; $n2weight = "25"; $n3weight = "50"; $n2diff = $n1weight + $n2weight; $n3diff = $n2diff + $n3weight; $num = rand(1, 100); echo "<br><b>Percent Test</b><br>"; $loop=1; $n1num=0;$n2num=0;$n3num=0; while($loop <= 100){ $num = rand(1, 100); if($num <= $n1weight) {$n1num++;} if($num > $n1weight && $num <= $n2diff) {$n2num++;} if($num > $n2diff && $num <= $n3diff) {$n3num++;} $loop++; } echo "Percents:<br> $name1: $n1num"."%"."<br>". "$name2: $n2num"."%"."<br>". "$name3: $n3num"."%"."<br>"; $totalnum = $n1num + $n2num + $n3num; echo "<b>Total: $totalnum</b>"; The amount of times a particular name is called varies around 2-6 give or take. Obviously it always add up to a hundred, but the figures are unreliable. Ignore experimental syntax..I'm still learning :-) Is there a more reliable way of doing this? Thanks. Link to comment https://forums.phpfreaks.com/topic/126050-best-way-for-setting-frequency-percent/ Share on other sites More sharing options...
AndyB Posted September 27, 2008 Share Posted September 27, 2008 ... but the figures are unreliable. Would you care to define jut what that means? Remember that random is random. Link to comment https://forums.phpfreaks.com/topic/126050-best-way-for-setting-frequency-percent/#findComment-651819 Share on other sites More sharing options...
DamienRoche Posted September 27, 2008 Author Share Posted September 27, 2008 ... but the figures are unreliable. Would you care to define jut what that means? Remember that random is random. Sorry, what I mean is that the percentages I am generating are unreliable. Have you any suggestions? Thanks. Link to comment https://forums.phpfreaks.com/topic/126050-best-way-for-setting-frequency-percent/#findComment-651820 Share on other sites More sharing options...
AndyB Posted September 27, 2008 Share Posted September 27, 2008 Please provide concrete examples of 'unreliable' and 'reliable' in the context of your problem. I still have no understanding of what your expectations are. Link to comment https://forums.phpfreaks.com/topic/126050-best-way-for-setting-frequency-percent/#findComment-651841 Share on other sites More sharing options...
DamienRoche Posted September 27, 2008 Author Share Posted September 27, 2008 again, I'm sorry if I'm not being specific enough. The output I receive from the script I wrote is: Damien: 22 Liam: 27 Deiter: 51 And those figures vary each time the script is run (5 or so for each line, give or take). What I want is: Damien: 20 Liam: 20 Deiter: 50 ...Every time. Ignore my comments about reliability. Have you got any suggestions? many thanks for your time. Link to comment https://forums.phpfreaks.com/topic/126050-best-way-for-setting-frequency-percent/#findComment-651850 Share on other sites More sharing options...
Barand Posted September 27, 2008 Share Posted September 27, 2008 What I want is: Damien: 20 Liam: 20 Deiter: 50 ...Every time. In that case <?php $name1 = "Damien"; $name2 = "Liam"; $name3 = "Deiter"; $n1weight = "25"; $n2weight = "25"; $n3weight = "50"; for ($i=1; $i<=3; $i++) { $n = ${'name'.$i}; $w = ${'n'.$i.'weight'}; echo "$n : $w<br/>"; } ?> Link to comment https://forums.phpfreaks.com/topic/126050-best-way-for-setting-frequency-percent/#findComment-651851 Share on other sites More sharing options...
DamienRoche Posted September 28, 2008 Author Share Posted September 28, 2008 Sorry to bump this, but...really?? How does no one know what I'm talking about? I want to produce a possibility using the three 'weight' variables. To clarify. I have assigned a weight to each of the variables, name1, name2, and name3, ..those weights are 25, 25, 50, respectively. Now how do I make my script: -call name1 25% of the time -call name2 25% of the time -call name3 50% of the time I can't find any info on this.. I'm beginning to think it can't be done. I would appreciate someone to tell me so than take the piss. What I want is: Damien: 20 Liam: 20 Deiter: 50 ...Every time. In that case <?php $name1 = "Damien"; $name2 = "Liam"; $name3 = "Deiter"; $n1weight = "25"; $n2weight = "25"; $n3weight = "50"; for ($i=1; $i<=3; $i++) { $n = ${'name'.$i}; $w = ${'n'.$i.'weight'}; echo "$n : $w<br/>"; } ?> But wait, how about: echo "Damien: 20 Liam: 20 Deiter: 50"; ?? Link to comment https://forums.phpfreaks.com/topic/126050-best-way-for-setting-frequency-percent/#findComment-652148 Share on other sites More sharing options...
Barand Posted September 28, 2008 Share Posted September 28, 2008 When you are using random events but insist on the same outcome every time you deserve it. Toss coin Weightings: Heads 50; Tails 50; Now toss it 4 times and see how often you get HHTT (and not HHHT, HTTT, HHHH or TTTT). After an infinite number of tosses then you'll have an even 50:50. If you want it exact every time then you need to control the events. Create an array of 100 elements (25 ones, 25 twos, 50 threes) and shuffle the array. When you loop through it you'll get the right number of events for each name but with randomly distributed sequence. Link to comment https://forums.phpfreaks.com/topic/126050-best-way-for-setting-frequency-percent/#findComment-652264 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.