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 5 of 5
  1. #1
    Regular Coder
    Join Date
    Oct 2002
    Location
    USA
    Posts
    255
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Two randomized outputs

    I use the simple code below to generate random outputs.

    Code:
    <?php 
    $ads = array( 
    'item1','item2','item3');
    $rand = rand(0,2);
    echo $ads[$rand];  
    ?>
    However, I need to make a simple advancement.

    I need to generate two random outputs instead of one - like publishing two quotes/pictures/whatever next to each other at the same time.

    How can I do that?

    Thank you.

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    just call rand twice ..
    PHP Code:
    <?php 
    $ads 
    = array('item1','item2','item3');
    $max=count($ads)-1;

    echo 
    $ads[rand(0,$max)]; 
    echo 
    $ads[rand(0,$max)];
    ?>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    If you wanted them not to be the same you could do something like:

    PHP Code:
    <?php
    $array 
    = array(...);

    $rand1 mt_rand(0sizeof($array)-1);
    $rand2 mt_rand(0sizeof($array)-1);

    while (
    $rand1 == $rand2){
    $rand1 mt_rand(0sizeof($array)-1);
    $rand2 mt_rand(0sizeof($array)-1);
    }

    echo 
    $array[$rand1] . $array[$rand2];
    ?>

  • #4
    Regular Coder
    Join Date
    Oct 2002
    Location
    USA
    Posts
    255
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Thumbs up

    Thanks! Works great!

  • #5
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts


  •  

    Posting Permissions

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