ramdomizer script

Discussion in 'Programming' started by izlik, Mar 23, 2009.

  1. #1
    Hello. Im looking for some kind of script to randomise code what code to display when a page is loaded. For example if a page is loaded where i have have this script it displays "code1" and the next time the page is loaded "code2" maybe displays.

    Anyone know where i can find someting like this ?
     
    izlik, Mar 23, 2009 IP
  2. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #2
    koko5, Mar 24, 2009 IP
  3. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #3
    crivion, Mar 24, 2009 IP
  4. nirajkum

    nirajkum Active Member

    Messages:
    815
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #4
    how many different options you have for randomization . Well you can just need a formula and you can use time stamp and modulo function as some of their parameter ..
     
    nirajkum, Mar 24, 2009 IP
  5. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #5
    for javascript:

    
    <script type="text/javascript">
    var r=Math.floor(Math.random()*2) + 1;
    document.write("<script type='text\/javascript' src='script" + r + ".js'><\/script>");
    </script>
    
    Code (markup):
    where the two scripts are 'script1.js' and 'script2.js' ... some slight modifications can allow as many different scripts as necessary.
     
    camjohnson95, Mar 24, 2009 IP
  6. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #6
    It's best to avoid using javascript if possible because users might have their javascript disabled. So, processing the request on server-side is a better solution, ie: PHP, etc.

    - ads2help
     
    ads2help, Mar 25, 2009 IP
  7. skyfe

    skyfe Active Member

    Messages:
    256
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    63
    #7
    You could do this with PHP if that's what you want:

    
    <?php
    
    //just some example
    $codes = 5; //amount of codes that can be shown
    $code_show = mt_rand(1,$codes); //generate random code ID
    
    switch($code_show) {
    
       case '1':
       //show code 1
       break;
    
       case '2':
       //show code 2
       break;
    
       //... etc etc
    
    }
    
    ?>
    
    Code (markup):
    Just an example, if that's what you mean. Could also be done with a database containing all codes for example, with all unique IDs then let it get a random ID and show the code belonging to that ID.

    Skyfe.
     
    skyfe, Mar 25, 2009 IP
  8. xisleet

    xisleet Active Member

    Messages:
    72
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #8
    or

    
    <?php
    
    $item[0] = "blah.php";
    $item[1] = "blah1.php";
    $item[2] = "blah2.php";
    
    echo ($item[rand(0, 2)]);
    
    ?>
    
    PHP:
    that should be simple enough... basically do anything you want with it in php, you could use include insted of echo or whatever
     
    xisleet, Mar 26, 2009 IP
  9. CarmenKarma

    CarmenKarma Greenhorn

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #9
    What coding language do you need it up? You originally didn't specify.

    :)
     
    CarmenKarma, Mar 28, 2009 IP