Cruisecar Posted May 5, 2006 Share Posted May 5, 2006 Hey,Okay I will have a percentage (0-100%), I need to input that into a function and it to output true or false based on the percentage. Thanks Link to comment https://forums.phpfreaks.com/topic/9156-random-number/ Share on other sites More sharing options...
Cruisecar Posted May 5, 2006 Author Share Posted May 5, 2006 function random ($no) {//do some equation based on percentage, so eg. xx% chance of getting true,if($something = true){return true;}else{return false;}} Link to comment https://forums.phpfreaks.com/topic/9156-random-number/#findComment-33730 Share on other sites More sharing options...
Caesar Posted May 5, 2006 Share Posted May 5, 2006 Just so I understand, you just need to generate a random number between 1-100, correct? Link to comment https://forums.phpfreaks.com/topic/9156-random-number/#findComment-33734 Share on other sites More sharing options...
Cruisecar Posted May 5, 2006 Author Share Posted May 5, 2006 No, I will input a number - 0-100% and I need the script to return true or false based on the percentage.Percentage = 50% - 1/2 chance of it returning true,Percentage = 90% - 9/10 chance of it returning true,I think that should sum it up :D Thanks for the reply. Link to comment https://forums.phpfreaks.com/topic/9156-random-number/#findComment-33735 Share on other sites More sharing options...
Caesar Posted May 5, 2006 Share Posted May 5, 2006 Hmmm...then why the title? :-PAlso...you are being just a tad vague. What conditions need to be met in order for it to return true/false? What will determine the precentage? Are you just entering whole numbers which will represent a %? Or are these numbers an actual percentage of something? You would need to know that, in order for us to help point ya in the right direction. Just a small example but...you can do something like:[code]<?php $ognumber = $_POST['number']; //Or Whatever Method You're Using To Get This Value $percent = ($ognumber / 100); if($percent > .49) {echo"At Least 50%";} else {echo"Less Than Half, Mate.";}?>[/code] Link to comment https://forums.phpfreaks.com/topic/9156-random-number/#findComment-33741 Share on other sites More sharing options...
Cruisecar Posted May 5, 2006 Author Share Posted May 5, 2006 Okay, i'll attempt to re-explain, I guess I did it kind of crappy.I will have whole numbers, 0-100, and based upon those numbers I need a 1 or 0. So say the number was 40, there would be a 40/100 chance the script would return 1/true. Link to comment https://forums.phpfreaks.com/topic/9156-random-number/#findComment-33745 Share on other sites More sharing options...
Caesar Posted May 5, 2006 Share Posted May 5, 2006 I see...so you are not really looking for a random result...but rather, a result based on probablity? And you will be somehow defining the value (Or percent) of this probablity? (Example 5/10 = 50% chance the result will be returned as "true")Correct? Link to comment https://forums.phpfreaks.com/topic/9156-random-number/#findComment-33747 Share on other sites More sharing options...
Cruisecar Posted May 5, 2006 Author Share Posted May 5, 2006 Yes, Sorry for describing it so terribly - and especially the title! Link to comment https://forums.phpfreaks.com/topic/9156-random-number/#findComment-33748 Share on other sites More sharing options...
Mattyspatty Posted May 5, 2006 Share Posted May 5, 2006 hi, i noticed caeser hasnt replied, so ill try n help[code]function chance($input) { $rand= rand(0, 100) //get a number, 0 - 100 if($rand > $input) { /*if the number is more than random then its outside the percentage. e.g. $rang = 80 and $input = 60 then the rand is part of the 40%.*/ return 1; //outside the chance% } else { return 0; //inside the chance% }}[/code]im not 100% sure how you return values in PHP, ive kinda forgotten :P. but that should work good :) Link to comment https://forums.phpfreaks.com/topic/9156-random-number/#findComment-33754 Share on other sites More sharing options...
Cruisecar Posted May 5, 2006 Author Share Posted May 5, 2006 Your a genious, I knew all of the stuff you said - I just didn't piece it together. Thankyou! Link to comment https://forums.phpfreaks.com/topic/9156-random-number/#findComment-33755 Share on other sites More sharing options...
Mattyspatty Posted May 5, 2006 Share Posted May 5, 2006 [!--quoteo(post=371725:date=May 5 2006, 11:18 PM:name=Cruisecar)--][div class=\'quotetop\']QUOTE(Cruisecar @ May 5 2006, 11:18 PM) [snapback]371725[/snapback][/div][div class=\'quotemain\'][!--quotec--]Your a genious, I knew all of the stuff you said - I just didn't piece it together. Thankyou![/quote]happy to help :D if you can find the solved button, using it is much appreciated here! Link to comment https://forums.phpfreaks.com/topic/9156-random-number/#findComment-33756 Share on other sites More sharing options...
Cruisecar Posted May 5, 2006 Author Share Posted May 5, 2006 [!--quoteo(post=371726:date=May 5 2006, 11:19 PM:name=Mattyspatty)--][div class=\'quotetop\']QUOTE(Mattyspatty @ May 5 2006, 11:19 PM) [snapback]371726[/snapback][/div][div class=\'quotemain\'][!--quotec--]happy to help :D if you can find the solved button, using it is much appreciated here![/quote]Erm, where is it? Link to comment https://forums.phpfreaks.com/topic/9156-random-number/#findComment-33758 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.