0

I need four multiple random numbers without repeating. So i took an array for it. Can you help me where i have done wrong? I have 24 questions to come in random order, I need 4 questions per page, for this i took an array "$questions" and initially inserted 25 into it. then when ever i got a random number which is not in array, i am replacing that particular index with the random number. Where i have done wrong?

<?php
$questions = array(0);
for ($i = 0; $i < 24 ; $i++) {
$questions[$i]= ",";
}

$a="1";
$b="2";
$c="3";
$d="4";
//$a=rand(0, 23);
while(!in_array($a=rand(0, 23), $questions)) {
    $replacements = array($a => $a);
    $questions = array_replace($questions, $replacements);
    $max = sizeof($questions);
    if ($max==4) {
        break;
    }
    echo "<br>a=".$a."<br>";
    for ($i = 0; $i < 24 ; $i++) {
        echo $questions[$i];
    }
}
//echo "a=".$a."b=".$b."c=".$c."d=".$d;
?>
1
  • 3
    That's a whole lot of recurring code. Have you ever heard about DRY? Commented Mar 10, 2013 at 10:35

2 Answers 2

4

I'd suggest randomizing the complete array/set once then breaking it down into chunks and store those chunks (e.g. in $_SESSION).

<?php
$questions = data(); // get data
shuffle($questions); // shuffle data
$questions = array_chunk($questions, 4); // split into chunks of four
// session_start();
// $_SESSION['questions'] = $questions;
// on subsequent requests/scripts do not re-create $questions but retrieve it from _SESSION

// print all sets
foreach($questions as $page=>$set) {
    printf("questions on page #%d: %s\n", $page, join(', ', $set));
}

// print one specific set
$page = 2;
$set = $questions[$page];
printf("\n---\nquestions on page #%d: %s\r\n", $page, join(', ', $set));


// boilerplate function: returns example data
function data() {
    return array_map(function($e) { return sprintf('question #%02d',$e); }, range(1,24));
}
Sign up to request clarification or add additional context in comments.

2 Comments

I honestly don't understand what you tried there. Maybe you should explain your algorithm.
finally i have implemented, your algo volkerk. Thank you.
3

You could do something like this:

<?php

$archive = array();
$span = 23;
$amount = 4;
$i = 0;
while (true) {
    $number = rand(0, $span);             // generate random number
    if (in_array($number, $archive)) {    // start over if already taken
        continue;
    } else {
        $i++;
        $archive[] = $number;             // add to history
    }
    /*
      do magic with $number
    */
    if ($i == $amount) break;             // opt out at 4 questions asked
}

3 Comments

.. but I actually like VolkerK's solution better.
Hmm. I am just trying to implement in my way. I am thankful to you guys for your answers. Thanks @Volkerk and Thanks to you also
Thank you @caspar. I have tried your logic and implemented volkrek

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.