SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Thread: random array

  1. #1
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,656
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    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
    "Oh, and Jenkins--apparently your mother died this morning."

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,214
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    populate an array, call shuffle on it, loop over it and output each item

  3. #3
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,226
    Mentioned
    194 Post(s)
    Tagged
    2 Thread(s)
    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?

  4. #4
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,656
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    ok, thx
    "Oh, and Jenkins--apparently your mother died this morning."

  5. #5
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,656
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    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?
    "Oh, and Jenkins--apparently your mother died this morning."

  6. #6
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,226
    Mentioned
    194 Post(s)
    Tagged
    2 Thread(s)
    What do you mean by "grouped together", an array or cocatenated?

  7. #7
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,214
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $startArray range(111); // create an array containing the values 1 through 11
    shuffle($startArray); // shuffle them so they are in a random order
    echo join(', '$startArray); // output them to the screen 

  8. #8
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,656
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    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(111);
    shuffle($startArray);
    //how do I group the first three elements in the array?, array_chunk
    echo join(', '$startArray); 
    "Oh, and Jenkins--apparently your mother died this morning."

  9. #9
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,214
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lukeurtnowski View Post
    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(111);
    shuffle($startArray);
    //how do I group the first three elements in the array?, array_chunk
    echo join(', '$startArray); 
    Give me an example of your expected output.

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


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •