# random array

Printable View

• Jun 6, 2014, 17:30
lukeurtnowski
random array
How do I output the numbers, 1-11 in a random order like 5, 7, 4, 11, 2, 3, 1, 9, 6, 8, 10
• Jun 6, 2014, 17:44
cpradio
populate an array, call shuffle on it, loop over it and output each item
• Jun 6, 2014, 18:00
Mittineague
No offense intended Luke, but IMHO PHP has a fairly well documented manual http://www.php.net/manual/en/function.rand.php
Is there something that fails to do for your needs?
• Jun 6, 2014, 18:32
lukeurtnowski
ok, thx
• Jun 6, 2014, 18:48
lukeurtnowski
I'm trying to figure out the answer to this sample question...

1. In PHP, create a randomized array with values 1-11, where the first three elements are grouped together.
Example Output: 5, 7, 4, 11, 2, 3, 1, 9, 6, 8, 10

I know how to generate a random number from 1 to 11 (rand(1,11)) and dont even know where to start (the question doesnt even seem like something like that is doable) Can you give me a point in the right direction?
• Jun 6, 2014, 18:57
Mittineague
What do you mean by "grouped together", an array or cocatenated?
• Jun 6, 2014, 20:30
cpradio
PHP Code:

``` \$startArray = range(1, 11); // create an array containing the values 1 through 11shuffle(\$startArray); // shuffle them so they are in a random orderecho join(', ', \$startArray); // output them to the screen  ```
• Jun 8, 2014, 14:21
lukeurtnowski
I lloks like after the array is shuffled, the first three elements (1, 2, 3) are grouped together in the array?
Is this ok?
PHP Code:

``` \$startArray = range(1, 11);shuffle(\$startArray);//how do I group the first three elements in the array?, array_chunkecho join(', ', \$startArray);  ```
• Jun 8, 2014, 16:23
cpradio
Quote:

Originally Posted by lukeurtnowski
I lloks like after the array is shuffled, the first three elements (1, 2, 3) are grouped together in the array?
Is this ok?
PHP Code:

``` \$startArray = range(1, 11);shuffle(\$startArray);//how do I group the first three elements in the array?, array_chunkecho join(', ', \$startArray);  ```

Give me an example of your expected output.

Are you expecting to just see the first 3 random numbers?