View Full Version : Random Help

03-10-2007, 05: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.

03-10-2007, 05: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>";

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

03-11-2007, 03: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)

03-11-2007, 04:54 PM


03-11-2007, 04:58 PM


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