# Random number

11 replies to this topic

### #1 Cruisecar

Cruisecar
• Members
• Member
• 22 posts

Posted 05 May 2006 - 08:39 PM

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

### #2 Cruisecar

Cruisecar
• Members
• Member
• 22 posts

Posted 05 May 2006 - 08:52 PM

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

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

### #3 Caesar

Caesar
• Members
• 1,025 posts

Posted 05 May 2006 - 08:55 PM

Just so I understand, you just need to generate a random number between 1-100, correct?
PHP Ninja

### #4 Cruisecar

Cruisecar
• Members
• Member
• 22 posts

Posted 05 May 2006 - 09:03 PM

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 Thanks for the reply.

### #5 Caesar

Caesar
• Members
• 1,025 posts

Posted 05 May 2006 - 09:17 PM

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:

```<?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.";}

?>```

PHP Ninja

### #6 Cruisecar

Cruisecar
• Members
• Member
• 22 posts

Posted 05 May 2006 - 09:22 PM

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.

### #7 Caesar

Caesar
• Members
• 1,025 posts

Posted 05 May 2006 - 09:38 PM

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?
PHP Ninja

### #8 Cruisecar

Cruisecar
• Members
• Member
• 22 posts

Posted 05 May 2006 - 09:39 PM

Yes, Sorry for describing it so terribly - and especially the title!

### #9 Mattyspatty

Mattyspatty
• Members
• 76 posts
• LocationEngland :D

Posted 05 May 2006 - 10:12 PM

hi, i noticed caeser hasnt replied, so ill try n help
```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%
}
}
```

im not 100% sure how you return values in PHP, ive kinda forgotten . but that should work good
ok, so my PHP is a bit rusty... my replies are 90% ok.
the other 10% either have syntax errors or are the "hard solution"

I have a C++ problem with 'keybd_event' in certain applications. if you can help me PM me!
Needs motivation to create another quick cash website. I need a new TV

### #10 Cruisecar

Cruisecar
• Members
• Member
• 22 posts

Posted 05 May 2006 - 10:18 PM

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

### #11 Mattyspatty

Mattyspatty
• Members
• 76 posts
• LocationEngland :D

Posted 05 May 2006 - 10:19 PM

[!--quoteo(post=371725:date=May 5 2006, 11:18 PM:name=Cruisecar)--][div class=\'quotetop\']QUOTE(Cruisecar @ May 5 2006, 11:18 PM) [/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 if you can find the solved button, using it is much appreciated here!
ok, so my PHP is a bit rusty... my replies are 90% ok.
the other 10% either have syntax errors or are the "hard solution"

I have a C++ problem with 'keybd_event' in certain applications. if you can help me PM me!
Needs motivation to create another quick cash website. I need a new TV

### #12 Cruisecar

Cruisecar
• Members
• Member
• 22 posts

Posted 05 May 2006 - 10:30 PM

[!--quoteo(post=371726:date=May 5 2006, 11:19 PM:name=Mattyspatty)--][div class=\'quotetop\']QUOTE(Mattyspatty @ May 5 2006, 11:19 PM) [/div][div class=\'quotemain\'][!--quotec--]
happy to help if you can find the solved button, using it is much appreciated here!
[/quote]

Erm, where is it?

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users