#### Archived

This topic is now archived and is closed to further replies.

# Random number

## Recommended Posts

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

##### Share on other sites
function random (\$no) {
//do some equation based on percentage, so eg. xx% chance of getting true,

if(\$something = true){
return true;
}
else{
return false;
}
}

##### Share on other sites
Just so I understand, you just need to generate a random number between 1-100, correct?

##### Share on other sites
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.

##### Share on other sites
Hmmm...then why the title? :-P

Also...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]

##### Share on other sites
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.

##### Share on other sites
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?

##### Share on other sites
Yes, Sorry for describing it so terribly - and especially the title!

##### Share on other sites
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 :)

##### Share on other sites
Your a genious, I knew all of the stuff you said - I just didn't piece it together. Thankyou!

##### Share on other sites
[!--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!

##### Share on other sites
[!--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?