Minase Posted August 15, 2008 Share Posted August 15, 2008 anyone know how i can do this thing? i have 0 and 1 and 1 has just a 2 % procent of coming as result,0 has 98 (it come almoust everytime) anyone know how to do it?thanks Link to comment https://forums.phpfreaks.com/topic/119872-solved-percentage/ Share on other sites More sharing options...
unkwntech Posted August 15, 2008 Share Posted August 15, 2008 If your talking about using weighted random, where one number occurs, more often then the other then I can help but I don't understand your question. Link to comment https://forums.phpfreaks.com/topic/119872-solved-percentage/#findComment-617538 Share on other sites More sharing options...
Minase Posted August 15, 2008 Author Share Posted August 15, 2008 first lets say i have 2 variables $a = 0; $b = 1; $ap = 2; // variable $a procent $bp = 98 // variable $b procent i want a random result ,but based on defined procent like rand(0,1); and 0 to have 2% chance and 1 98% thank you Link to comment https://forums.phpfreaks.com/topic/119872-solved-percentage/#findComment-617540 Share on other sites More sharing options...
unkwntech Posted August 15, 2008 Share Posted August 15, 2008 This is a weighted random, I'll post back in a few minutes, I have a function for this, but I need to find it. Link to comment https://forums.phpfreaks.com/topic/119872-solved-percentage/#findComment-617541 Share on other sites More sharing options...
Minase Posted August 15, 2008 Author Share Posted August 15, 2008 ok thank you very much Link to comment https://forums.phpfreaks.com/topic/119872-solved-percentage/#findComment-617543 Share on other sites More sharing options...
Barand Posted August 15, 2008 Share Posted August 15, 2008 <?php $ar = array (0,0); $ar = array_pad($ar,100,1); shuffle($ar); for ($i=0; $i<50; $i++) { if ($ar[array_rand($ar)]==1) { echo "1", '<br/>'; } else echo "0", '<br/>'; } ?> Link to comment https://forums.phpfreaks.com/topic/119872-solved-percentage/#findComment-617616 Share on other sites More sharing options...
corbin Posted August 15, 2008 Share Posted August 15, 2008 Couldn't you just do: <?php $r = rand(0, 99); $number = ($r < 2) ? 0 : 1; There are 2 numbers less than 2 (0 and 1), therefore 2/100 = 2% chance. There are 98 numbers not less (in other words, greater than or equal to) than 2, so there's a 98% chance of 1. Or is this way not random enough or something? Link to comment https://forums.phpfreaks.com/topic/119872-solved-percentage/#findComment-617635 Share on other sites More sharing options...
Minase Posted August 16, 2008 Author Share Posted August 16, 2008 thanks Link to comment https://forums.phpfreaks.com/topic/119872-solved-percentage/#findComment-617936 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.