DerekAllard.com

Using PHP for random, non-repeating numbers

Need a series of non-repeating but random numbers? For example, random images from a gallery or portfolio screenshots? Let me save you some time:

$numbers range($min$max);
shuffle($numbers);

$random_1 $numbers[0]// first random number
$random_2 $numbers[1]// next random, non repeating
$random_3 $numbers[2]// next random, non repeating 

Normally I wouldn’t blog about something like that, except that I just spent 20 minutes using rand() to generate a random number, then saved it into an array, then ran an in_array() to look for it, and start the process over.

It wasn’t until I was about 15 minutes into it that I thought to myself… “What on Earth am I doing?”.  Then stubbornness kicked in and I tried to finish my monstrosity, then I finally re-evaluated my strategy.

Funny, because I’ve never used shuffle() before for anything, so my brain could only think of commonly used array functions like sort() and asort() (and of course, your favourite and mine… array_intersect_uassoc() - which I freely admit to only just now looking up to discover it exists).

Why are we sometimes driven to code the most complex solution possible, and worse, even when we know it is utterly involved and inelegant, we still drive to make it work, “just to see it working”?

This entry was made on and filed into How-To, PHP.

Comments

Richard wrote on

..very handy as I was just about to do the same thing!

Also, I downloaded a copy of Bambooinvoice (I am using CI for a few projects, so thought I might get some inspiration) and was surprised to see an (int) cast used. I use casting myself (having used it in C) but I’ve never seen it in another php programme. 

I wonder why more people don’t use it?

john wrote on

I used something similar on a random haiku generator.

$line_1 = ('example text1''example text2''example text3');  // first line of haiku 
$line_2 = ('example text4''example text5''example text6');  // second line of haiku
$line_3 = ('example text7''example text8''example text9');  // third line of haiku


shuffle($line_1); // mix up the contents
shuffle($line_2); // mix up the contents
shuffle($line_3); // mix up the contents

$number1 count($line_1);  // this may be wrong I am doing this from memory
$number2 count($line_2);  // but you calculate the length of the array
$number3 count($line_3);  // and place it in a variable

$random_1 rand(0$number1);  // pull random number from array count for the first array
$random_2 rand(0$number2);  // etc
$random_3 rand(0$number3);  // etc

$first_line $line_1[$random_1]
$second_line $line_2[$random_2]
$third_line $line_3[$random_3]

echo 
"$first_line"// then print them out
echo "$second_line"// then print them out
echo "$third_line"// then print them out 

sorry if this doesn’t work quite right, I think you get the idea.

Shadowhand wrote on

That is a beautifully elegant solution, Derek. The only issue I see with this:

If I want 5 random images from my “gallery” table, the numbers might not be in order.

The simple solution to this problem? Do a quick query, like this:

$query = $this->db->select(‘id’)->limit(5)->orderby(‘RAND()’)->get(‘gallery’);

Now I’ve achieved the same effect, having a set of random ID numbers that I can use query for. Obviously this could be simplified to just fetch the objects I need, eliminating the need for a second query.

Sacramento Printing wrote on

I know exactly what you mean. I spent a solid month creating a sudoku solver in c++ and even using my own data structures and functions (push, pop, etc.). I utilized some of the code I had used for a maze traversal program I made for a class. I knew there were a million other 100% more efficient sudoku solvers out there that could solve much harder puzzles than mine but there is a certain satisfaction in doing something yourself.

Jason Falk wrote on

Hey thanks for this, I remember seeing a php code similar to this some months back but did not bookmark the page. This is a great idea and can save a lot of manual work.