Solved

Generating a set of random unique numbers

Posted on 2016-11-17
6
37 Views
1 Endorsement
Last Modified: 2016-11-18
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

1
Comment
Question by:burnedfaceless
  • 4
6 Comments
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 41892127
PHP my_rand() is good enough.  You might also want to consider using range() and shuffle().  You can shuffle more than once, if you'd like.

In an application like this, don't let the great be the enemy of the good.  You're not in need of cryptographic excellence.  You just need random-enough numbers!
0
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 500 total points
ID: 41892135
You might take some inspiration from this.  You could repackage it as a function that returns the array of numbers.

Also, congratulations on the decision to quit smoking.  It's not easy, but it's worth it for all kinds of reasons.  I'm speaking as a born-again non-smoker.  You're doing the right thing.
https://iconoun.com/demo/temp_burnedfaceless.php
[code]<?php // demo/temp_burnedfaceless.php.php
/**
 * https://www.experts-exchange.com/questions/28983854/Generating-a-set-of-random-unique-numbers.html
 */
error_reporting(E_ALL);
echo '<pre>';

$kount = 32;
while($kount)
{
    $kount--;
    
    $row = range(1,24);
    shuffle($row); // RANDOMLY ORDERED UNIQUE NUMBERS FROM 1 -- 24
    
    $txt = implode(',', $row);
    print_r($txt);
    echo PHP_EOL ;
}

Open in new window

[/code]
0
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 500 total points
ID: 41892140
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
LVL 34

Expert Comment

by:gr8gonzo
ID: 41892213
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.
2
 

Author Closing Comment

by:burnedfaceless
ID: 41892469
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]
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 41892777
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.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question