Jump to content

[SOLVED] simple random function


noon

Recommended Posts

I need a function that accepts 1 parameter, $max.  The function will return an array of numbers from 1 to $max (inclusive) in random order, with no repeats.  Ex: echo rand_group(4); // 3,1,2,4

 

The only approach I can think of right now is generating the random, checking if its already in my array, add if not, generate again if it is. This brute force is quite inefficient I am sure. Wondering if anyone can help me write a better piece of code. For considerations, let's say the max number would never go above 10.

Link to comment
https://forums.phpfreaks.com/topic/110438-solved-simple-random-function/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.