By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,650 Members | 1,785 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,650 IT Pros & Developers. It's quick & easy.

Randomly selecting an element in an array that has not been selected before..

P: n/a
Hi,
Im having problems in randomly selecting an element in an array that
has not been selected before.. in other words, I have an array of
answers to questions, then I want to select 5, with one of them being
the correct answer to the question, and the others not..
Here is my code.. (I already have the correct answer at array position
[0])

for($i=1;$i<=4;$i++)
{
//choose a random number in array range
$phraseArr_random_answer=rand(1,$phraseArr_size);
// check to see if random selection has not already been chosen

for($j=0;$j<=$i;$j++)
{
//check to see if element has already been chosen (j is current
array)
echo">> ".$answerArr[$j][2] ."<< >>".
$phraseArr[$phraseArr_random_answer][2]."<< <br />";
if(strcmp($answerArr[$j][2],$phraseArr[$phraseArr_random_answer][2]))
{
$alreadyUsed="FALSE";
$i--;
//document.writeln("choices[j] = " + choices[j] + " and i = " + i
// + " and viet[random_choice] = " + viet[random_choice] + "<br
/>");
//break;
echo"test";
}
else
{
$alreadyUsed="TRUE";/*
}
}
//add a random selection from phraseArr into each element of
answerArr
if ($alreadyUsed=="TRUE")
{
$answerArr[$j][2]=$phraseArr[$phraseArr_random_answer][2];
}

$answerArr[$i]=$phraseArr[$phraseArr_random_answer][2];
echo $answerArr[$i]." - ";
}

Thanks to anyone who can tell me the problem with this..
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On 28 Jun 2004 06:39:13 -0700, ad*******@yahoo.com (Joe Six-Pack)
wrote:
Im having problems in randomly selecting an element in an array that
has not been selected before.. in other words, I have an array of
answers to questions, then I want to select 5, with one of them being
the correct answer to the question, and the others not..
Here is my code.. (I already have the correct answer at array position
[0])


why not use the shuffle-function to mix up the array?

Here is an example:

<?
$questions = array(
array("correct answer", 1),
array("false1", 0),
array("false2", 0),
array("false3", 0),
array("false4", 0)
);

srand ((float)microtime()*1000000);
shuffle($questions);

echo "<pre>";
print_r($questions);
echo "</pre>";

$chosen = $questions[3];
echo "you chose: $chosen[0]<br>";

if ($chosen[1] == 1)
{
echo "correct answer<br>";
} else {
echo "wrong answer<br>";
}
Regards

Marian

--
Tipps und Tricks zu PHP, Coaching und Projektbetreuung
http://www.heddesheimer.de/coaching/
Jul 17 '05 #2

P: n/a
"Marian Heddesheimer" <26*************@spamgourmet.com> wrote in message
news:6o********************************@4ax.com...
On 28 Jun 2004 06:39:13 -0700, ad*******@yahoo.com (Joe Six-Pack)
wrote:
Im having problems in randomly selecting an element in an array that
has not been selected before.. in other words, I have an array of
answers to questions, then I want to select 5, with one of them being
the correct answer to the question, and the others not..
Here is my code.. (I already have the correct answer at array position
[0])


why not use the shuffle-function to mix up the array?


Or use array_rand() to random pick out an element. Then use array_splice()
to remove that has been selected.
Jul 17 '05 #3

P: n/a
On Mon, 28 Jun 2004 06:39:13 -0700, Joe Six-Pack wrote:
Hi,
Im having problems in randomly selecting an element in an array that has
not been selected before.. in other words, I have an array of answers to
questions, then I want to select 5, with one of them being the correct
answer to the question, and the others not.. Here is my code.. (I already
have the correct answer at array position [0])


The algorithm is as follows:

$n = size( $array );
while ( $n > 0 )
{
// generate a random number from 0 to $n
$i = rand(0, $n );
$temp = $array[$i];
$array[$i] = $array[$n - 1];
$array[$n - 1] = $temp;
$n--;
}

Of course, I'm not sure that this loop runs any faster than shuffle.

As always, make sure your script seeds the random number generator before
calling rand or shuffle.

What you have to decide is what to shuffle - the array of possible answers
or an array of indexes into the array of possible answers (in this case,
use array_rand, passing in size($array) as the second parameter).

HTH,
La'ie Techie

Jul 17 '05 #4

P: n/a
ad*******@yahoo.com (Joe Six-Pack) wrote in message
news:<48**************************@posting.google. com>...

Im having problems in randomly selecting an element in an array that
has not been selected before..


Randomness has NOTHING to do with whether an element has been selected
before. If your array has n elements, there is a (1/(n*n)) chance that
the next choice will be the same as last.

Cheers,
NC
Jul 17 '05 #5

P: n/a
Nikolai Chuvakhin wrote:
ad*******@yahoo.com (Joe Six-Pack) wrote in message
news:<48**************************@posting.google. com>...

Im having problems in randomly selecting an element in an array that
has not been selected before..
Randomness has NOTHING to do with whether an element has been selected
before. If your array has n elements, there is a (1/(n*n)) chance that
the next choice will be the same as last.


I think you parsed his request wrong, Nikolai: :-)
problem: randomly selecting (an element in an array
that has not been selected before).


I.e., sampling without replacement.

Chung's suggestion is a good --and might be the fastest; I certainly
haven't found a faster-- way to do that.

Margaret
--
(To mail me, please change .not.invalid to .net, first.
Apologies for the inconvenience.)
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.