Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14

Thread: Random Order

  1. #1
    CSS
    CSS is offline
    New Coder
    Join Date
    Jun 2006
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Random Order

    How would you make a randomizer where you pick 6 words and it randomizes them in order so that no word is repeated?

  • #2
    Regular Coder devinemke's Avatar
    Join Date
    Dec 2004
    Location
    NYC
    Posts
    443
    Thanks
    0
    Thanked 12 Times in 11 Posts
    PHP Code:
    $words = array(
    'PHP',
    'is',
    'the',
    'greatest',
    'thing',
    'yet'
    );

    shuffle($words);
    echo 
    implode(' '$words); 

  • #3
    CSS
    CSS is offline
    New Coder
    Join Date
    Jun 2006
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks a bunch.

  • #4
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    PHP Code:
    $words = array("elephant","yak","dog","cat","sheep","spookster");
    $random array_rand($words6);
    for(
    $i=0;$i<6;$i++){
       echo 
    $words[$random[$i]];

    Something like that

  • #5
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    It is better to use the most logical function, as it makes your code easier to read.

    array_rand() is used to pick a few random elements from an array. It can be used to shuffle (!) the array but the manual does not state that the elements will be returned in a randomized order. Only that a random element be picked.

  • #6
    CSS
    CSS is offline
    New Coder
    Join Date
    Jun 2006
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Neither of those worked...

  • #7
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Show the code that you tried it with. So that we can see what the problem is. There's no reason that those functions should not have worked.

  • #8
    CSS
    CSS is offline
    New Coder
    Join Date
    Jun 2006
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Does anyone know how to do this?

  • #9
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    The 2 examples shown above WILL do this. You've done something wrong, but we cannot say what you've done wrong until you show your code

  • #10
    CSS
    CSS is offline
    New Coder
    Join Date
    Jun 2006
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, I tried both within PHP tags and both showed blank pages.

  • #11
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Show...

    us....

    your....

    code.....

  • #12
    CSS
    CSS is offline
    New Coder
    Join Date
    Jun 2006
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <?php
    $words = array("elephant","yak","dog","cat","sheep","spookster");
    $random = array_rand($words, 6);
    for($i=0;$i<6;$i++){
       echo $words[$random[$i]];
    }  
    ?php>

  • #13
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    PHP Code:
    <?php
    $words 
    = array("elephant","yak","dog","cat","sheep","spookster");
    $random array_rand($words6);
    for(
    $i=0;$i<6;$i++){
       echo 
    $words[$random[$i]];
    }  
    ?>
    The closing of php is ?> not ?php>

    If you posted the code several posts ago like you was asked, you could've gotten the answer much quicker.

    Also, next time you have a problem, it'll help to show the error. You should've got something similar to
    Parse error: parse error, unexpected '?' in var/www/test.php on line 7

  • #14
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Tested devinemke's solution and it works.


  •  

    Posting Permissions

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