Jump to content

Is there anything like a random function?


iStriide

Recommended Posts

I'm wanting to know if there is a random function in PHP. I am not sure if there is one, but I could I do something where I can make something a have lets say a 25% chance of happening.

 

Example: Can I do anything like this?

25% chance of echoing "Whats Up" or 75% chance of echoing "I hate lag".

 

If there is anything like this any PHP I would appreciate if you guys would tell me how to use it.

<?php
$variable = array("whats up","whats up","I hate lag","I hate lag","I hate lag");
$p1 = 0;
$p2 = 0;
for($i=0;$i<=100;$i++){


   shuffle($variable);
   if($variable[0] == "whats up"){ $p1++; } else { $p2++; }
   
   
}
print $p1 . '<br>';
print $p2;
  ?>  

 

 

34 - Whats up
67 - I hate Lag

25- Whats up
76- I hate Lag

37- Whats up
64 - I hate Lag

$arr = array('whats up', 'I hate lag');
$rand = rand(1,25); // 25% chance max
$remainder = 100 - $rand;
echo $arr[0].' : '.$rand.'%<br />';
echo $arr[1].' : '.$remainder.'%<br />';

 

I would just to point out that this is not valid. Because Whats up and I Hate lag each half a 50/50 chance of happening in reality, while your only producing percentages not based on the array. OP's question wasn't very clear, because you would need to actually check how many times whats up and i hate lag occur based on the array elements.

 

Just a heads up.

 

I like your method though

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.