5 different rand()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fariquzeli
Forum Contributor
Posts: 144
Joined: Mon Jun 24, 2002 9:16 am
Location: Chicago
Contact:

5 different rand()

Post 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?
User avatar
puckeye
Forum Contributor
Posts: 105
Joined: Fri Dec 06, 2002 7:26 pm
Location: Joliette, QC, CA
Contact:

Re: 5 different rand()

Post 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
fariquzeli
Forum Contributor
Posts: 144
Joined: Mon Jun 24, 2002 9:16 am
Location: Chicago
Contact:

Post 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 :(
User avatar
gyardleydn
Forum Commoner
Posts: 27
Joined: Tue Dec 03, 2002 8:27 am

Re: 5 different rand()

Post 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;
fariquzeli
Forum Contributor
Posts: 144
Joined: Mon Jun 24, 2002 9:16 am
Location: Chicago
Contact:

Post 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;
?>
User avatar
Traduim
Forum Newbie
Posts: 11
Joined: Tue Dec 10, 2002 4:21 am
Location: Catalunya

Post 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.
fariquzeli
Forum Contributor
Posts: 144
Joined: Mon Jun 24, 2002 9:16 am
Location: Chicago
Contact:

Post 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.
User avatar
Traduim
Forum Newbie
Posts: 11
Joined: Tue Dec 10, 2002 4:21 am
Location: Catalunya

Post by Traduim »

You can use a where clause in conjunction with order by and limit.
fariquzeli
Forum Contributor
Posts: 144
Joined: Mon Jun 24, 2002 9:16 am
Location: Chicago
Contact:

Post by fariquzeli »

would that return multiples of the same integer? ex. two number 4's?
User avatar
Traduim
Forum Newbie
Posts: 11
Joined: Tue Dec 10, 2002 4:21 am
Location: Catalunya

Post 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.
fariquzeli
Forum Contributor
Posts: 144
Joined: Mon Jun 24, 2002 9:16 am
Location: Chicago
Contact:

Post by fariquzeli »

works very well. Alot simpler too and it will make it much easier to display all the records. Thanks a bunch!
Post Reply