Link to home
Start Free TrialLog in
Avatar of burnedfaceless
burnedfaceless

asked on

Generating a set of random unique numbers

I want to be able to administer quiz questions in a random unique order. I think one way to do this would be to use the array shuffle function, but I read that it was not truly random.

I thew together a quiz and wrote the function below. Is the rand function truly random? I looked at the manual and I saw random_int but it said "pseudo random".

I'm trying to quit smoking and doing a poor job articulating myself but I hope this is enough to understand what I'm trying to do.

What about mt_rand? http://php.net/manual/en/function.mt-rand.php

If I wanted to write professional level code would I be better off writing a function that generates a truly random number?

How would I go about that, please don't tell me everything or give me something complete, but if you could get me started thinking I would appreciate it (even if I use mt_rand I think I should try to understand how to generate a random number).

Thank you very much.

<?php

function generateNumbers($max) {
  $number = new SplFixedArray($max);
  $j = 0;
  $number[$j] = rand(1, 68);
  $j++;
  $loopLimit = $max - 1;
  
  while ($j <= $loopLimit) {
    $generatedNumber = rand(1, $max);
    for ($i = 0; $i <= $j; $i++) {
      if ($number[$i] == $generatedNumber) {
        break;
      }
      elseif ($i == $j) {
        $number[$j] = $generatedNumber;
        $j++;
        break;
        }
      }
    }
    return $number;
}

?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gr8gonzo
1. How important is it that each number be unique?

2. If it's critical, then is there a "lifespan" to the number? For example, if you generate "3", will it be okay to generate "3" again an hour from now when you run your script again, or can "3" never, ever be used again?

3. How important is it that each number be truly random, and are there any min/max requirements on the number? For example, if you had "1", "2", "3", "123213429676734", would that be okay?

4. Most importantly of all, what's the number ultimately going to be used for? This question along might allow us to provide some better advice/guidance.
Avatar of burnedfaceless
burnedfaceless

ASKER

Thanks for posting but Ray managed to translate my jibberish and answer my question - I'm letting the great be the enemy of the good when what I've written serves the purpose at hand (basically randomizing all quizzes and multiple choice selectors, but more crucially some scripts will contain material that will need to be treated like flashcards, and this allows me to shuffle the flashcards which is important, more important the more you do). I do think I can improve on this - if a subject is learned through thinking through it (rather than rote memorization) then it's not important to isolate individual sections - you can run through the entire material because they're working on thought processes and the memorization will come for some, but ultimately the way to learn that material is to work through it understanding how to solve it, then the memorization will come. Even for that kind of material I think you can tag a particular one that someone had trouble with and allow them to review it sooner, I'll have to test it out - bunch of music theory that I've more or less forgotten so it should be a good test.

Anyways I bummed a smoke - my brain is working again. Thanks for the encouragement Ray, I got back on Chantix. Made it one month with Chantix then the cravings came back (really weird after about 10 days) and I thought I could handle them but they got so strong I had to smoke.

I'll do what it takes to quit. Another try makes me that much more likely to stop.

[b]Ray I'm not sure if you're reading this because it's quite the essay but I'm going to try to do frontend work (unless I got a job doing something else) soon. If I get a job in town it would be ideal because I could attend school and take the two intro level programming classes - I am 90% sure they are Java - then I could get the PHP class to count as an upper level elective and have a degree. I want to be able to work through this book http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.691.3733&rep=rep1&type=pdf. Should I learn C++ right now along with HTML5 and CSS3? Is C++ a good language to know?[/b]
About those cravings... They go on for at least a year, so don't be surprised or disappointed that a month or three is not enough to break the chains.  Persistence is a virtue.  I found the cravings to be triggered by associative events.  Even a full year after quitting, I went water skiing and when I jumped back into the boat I had a sudden and familiar craving for the cigarette I had always smoked after skiing!  It took me a lot of tries to stop completely.  Everyone I know who has quit has had a similar experience.  You'll get there, even if the path is not always straight to the goal.

About C++ ... Sure, it's definitely worth understanding, at least well enough to read code and make an educated guess about what it is doing.  You don't have to develop a depth of knowledge, but it's one of many useful languages that can help to shape your thinking about information problems.  From a practical viewpoint, a depth of knowledge in JavaScript will be very useful.