Re: Diferent numbers
- From: "Andrew @ Rockface" <andrew@xxxxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 24 Jun 2005 13:16:23 +0000 (UTC)
In news:d9gp2c$j75$1@xxxxxxxxxxxxxx,
CyberDog <milan-SPAM-@xxxxxxxxxxxx> wrote:
> Andrew @ Rockface wrote:
>> As each number is generated record it in an array, then check each
>> new number against the elements in the array.
>
> I would like to go with this
>
> Could you be so kind and write an example for me?
>
> is it something like this:
>
> while ($postition < $displayed_questions)
> {
> $question = array(
> $number => rand(1, $questions_in_database)
> )
> $number++
> }
>
> How to check if the number is used? While in a while?
I think the shuffle is the best way, unless you've got a massive amount of
questions :)
$questions_in_database = 10;
$questions = range(0,$questions_in_database);
shuffle($questions);
for ($c = 0; $c < 5; $c++) {
echo "RANDOM NUMBER =".$questions[$c]."<br>";
}
If you want to use the other method:
$questions_in_database = 5;
$questions = array();
for ($c = 0; $c < 5; $c++) {
$rnd = mt_rand(0,$questions_in_database);
if (! in_array($rnd, $questions)) {
$questions[] = $rnd;
}
}
echo "<pre>";print_r($questions);echo "</pre>";
--
Andrew @ Rockface
np: 1000 Suns - 5 [stopped]
www.rockface-records.co.uk
.
- Follow-Ups:
- Re: Diferent numbers
- From: CyberDog
- Re: Diferent numbers
- References:
- Diferent numbers
- From: CyberDog
- Re: Diferent numbers
- From: Andrew @ Rockface
- Re: Diferent numbers
- From: CyberDog
- Diferent numbers
- Prev by Date: Re: MS Access Equivalent
- Next by Date: Re: <img width > in apache
- Previous by thread: Re: Diferent numbers
- Next by thread: Re: Diferent numbers
- Index(es):
Relevant Pages
|