Page 1 of 1

5 different rand()

Posted: Wed Dec 11, 2002 2:29 pm
by fariquzeli
I'm looking to make the first 5 products on my main page random numbers between 2 and nine, so it will look something like this:

Code: Select all

$first = rand(2,9);
$second = rand(2,9);
$third = rand(2,9);
$fourth = rand(2,9);
$fifth = rand(2,9);
My question is, how do I make it so that when getting a random integer, there are no duplicates, because I only want each product to show up once. How do I make it so that number 2 doesn't come up twice as a random integer?

Re: 5 different rand()

Posted: Wed Dec 11, 2002 2:47 pm
by puckeye
Pretty much the only way I can see is to check the second throuh fifth to make sure there's no duplicate.

Code: Select all

<?php
function random($min, $max, $taken) // $taken is an array;
{
    do
    {
        $draw = rand($min, $max);
    } while(in_array ( $draw, $taken));
    $taken[count($taken)] = $draw;  // add the new value in the array
   return $draw;
}

$taken = array();

$first = random(2, 9, $taken);
$second = random(2, 9, $taken);
$third = random(2, 9, $taken);
$fourth = random(2, 9, $taken); 
$fifth = random(2, 9, $taken);

?>
I think this will do the trick. This is untested though.

Michel

Posted: Wed Dec 11, 2002 3:04 pm
by fariquzeli
didn't work, returns multiples :(

i echoed out all the variables and got

54452

2 4's and 2 5's that's no good :(

Re: 5 different rand()

Posted: Thu Dec 12, 2002 12:20 am
by gyardleydn
puckeye wrote:Pretty much the only way I can see is to check the second throuh fifth to make sure there's no duplicate.

Code: Select all

<?php
 function random($min, $max, $taken) // $taken is an array;
Global array will not be changed. Minor glich, you need a reference here. try:

Code: Select all

<?php
function random($min, $max, &$taken) // $taken is an array;

Posted: Thu Dec 12, 2002 12:36 am
by fariquzeli
I figured it out, I just wrote it out, wasn't soo bad:

Code: Select all

<?php
$first = rand(2,9);
do {
$second = rand(2,9);
}
while( $second == $first);

do {
$third = rand(2,9);
}
while( $third == $first | $third == $second);

do {
$fourth = rand(2,9);
}
while( $fourth == $first | $fourth == $second | $fourth == $third);

do {
$fifth = rand(2,9);
}
while( $fifth == $first | $fifth == $second | $fifth == $third | $fifth == $fourth);

echo $first;
echo $second;
echo $third;
echo $fourth;
echo $fifth;
?>

Posted: Thu Dec 12, 2002 11:05 am
by Traduim
Perhaps it's not the case, but if you want to recover 5 random records out a MySQL database, you just need to do one only SQL query:

Code: Select all

mysql_query("SELECT * FROM table ORDER BY RAND() LIMIT 5");
I hope it might be useful.

Posted: Thu Dec 12, 2002 11:11 am
by fariquzeli
the only thing is there's about 60 entries in the table and I want to select between 9 of them that have a special variable set.

It's alright though it all worked out in the end.

Posted: Thu Dec 12, 2002 11:52 am
by Traduim
You can use a where clause in conjunction with order by and limit.

Posted: Thu Dec 12, 2002 11:55 am
by fariquzeli
would that return multiples of the same integer? ex. two number 4's?

Posted: Thu Dec 12, 2002 12:02 pm
by Traduim
Example:

SELECT * FROM table WHERE nice = 1 AND psicothic = 0

It would select all the records matching the where clause, and it will not sort the results in any way.

SELECT * FROM table WHERE nice = 1 AND psicothic = 0 ORDER BY RAND()

The same as before, but the results have been shuffled at random.
If you run this select twice, you will have the same results sorted in two different ways

SELECT * FROM table WHERE nice = 1 AND psicothic = 0 ORDER BY RAND()

The same as before, but this time you just get the 5 top records.
If you run this select twice, you will have different results.

Simply give it a try and see if it works.
There are some cases where this is not functional, but it saves a lot of time in several cases.

Posted: Thu Dec 12, 2002 12:23 pm
by fariquzeli
works very well. Alot simpler too and it will make it much easier to display all the records. Thanks a bunch!