...

View Full Version : Random Help



ptmuldoon
03-10-2007, 06:07 PM
I'm trying to learn how to create a random script, but I want the random script to to only include specific numbers, and then generate a random value of one those numbers.

Essentially, I want values, of 1, 5, 8, 17, 23, 100, 41 etc. And then to randomly generate a number.

Eventually, I'll look to weight the probability of the number being randomly selected.

ptmuldoon
03-10-2007, 06:49 PM
Well, I learned I can do this to generate a random number of specific values. But I'm not sure yet how to possibly weight any of numbers.

$numbers = array ("4", "6", "8", "10", "12", "15", "20", "25", "50", "100");

srand ((double) microtime() * 1000000);
$randomnumber = rand(0,count($numbers)-1);

echo "<p>" . $numbers[$randomnumber] . "</p>";

Inigoesdr
03-10-2007, 07:58 PM
Use array_rand() (http://www.php.net/array_rand), you can seed it if you want.

rafiki
03-11-2007, 04:55 PM
$number = array(1,4,5,6,7,8,22,12,34,45,2);
$i = rand(0, arraymax);
$shuffle = shuffle($number);
echo "$shuffle[$i]";

that would show a random number from the array although change $arraymax to how many numbers are in your array -1 (because array starts at 0)

the-dream
03-11-2007, 05:54 PM
use:

rand();

goughy000
03-11-2007, 05:58 PM
use:

rand();

he wants a pre defined set of values that will be chosen from at random so this wont work

http://uk.php.net/array_rand is your best bet



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum