Need a random number generator

Discussion in 'Programming' started by eagleout, Aug 23, 2009.

  1. #1
    I need a script (php, java, etc.) to generate a random four-digit, alphanumeric number at the press of a button.

    It should be able to be added to a webpage and display the result next to the link/button in standard text. Nothing fancy.

    Respond here or PM offers/example.

    Thanks-
     
    eagleout, Aug 23, 2009 IP
  2. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #2
    <?php
    if (!empty($_POST)) {
    	$random_string = random_string(4);
    } else {
    	$random_string = '';
    }
    
    function random_string($len, $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789') {
        $string = '';
        for ($i=0; $i<$len; $i++) {
            $pos = mt_rand(0, strlen($chars)-1);
            $string .= $chars{$pos};
        }
        return $string;
    }
    ?>
    <html>
    <body>
    <?php if ($random_string): ?>
    The random number is: <?php echo $random_string; ?>
    <?php endif; ?>
    <form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" style="display:inline">
    	<input type="submit" name="submit" value="Generate" />
    </form>
    </body>
    </html>
    Code (markup):
     
    premiumscripts, Aug 23, 2009 IP
  3. eagleout

    eagleout Active Member

    Messages:
    221
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #3
    Can that be done in AJAX?
     
    eagleout, Aug 23, 2009 IP
  4. webmasterplace

    webmasterplace Peon

    Messages:
    802
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I can do this.
    PM me ;)
     
    webmasterplace, Aug 23, 2009 IP
  5. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #5
    test.php

    <html>
    <head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>
    <body>
    <p>Random number: <span id="random_number"></span> <a href="#" onclick="$('#random_number').load('ajax.php');return false;">load</a></p>
    </body>
    </html>
    Code (markup):
    ajax.php

    <?php
    function random_string($len, $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789') {
        $string = '';
        for ($i=0; $i<$len; $i++) {
            $pos = mt_rand(0, strlen($chars)-1);
            $string .= $chars{$pos};
        }
        return $string;
    }
    
    echo random_string(4);
    ?>
    Code (markup):
     
    premiumscripts, Aug 23, 2009 IP
  6. eagleout

    eagleout Active Member

    Messages:
    221
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #6
    Works beautifully. A thousand thanks.
     
    eagleout, Aug 23, 2009 IP
  7. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Though now that I think about it, this could just as easily be done in regular javascript :) It's because you asked for a php solution first that I did not consider that. Ehh, it works just the same really.
     
    premiumscripts, Aug 23, 2009 IP
  8. CrunchyToast

    CrunchyToast Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #8
    I know this thread is old, but can anyone tell me how to generate this random number into a text field via the ajax script?
     
    CrunchyToast, Jan 19, 2013 IP
  9. jimbo2779

    jimbo2779 Active Member

    Messages:
    105
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    80
    #9
      <input type="text" name="textfield" id="textfield" value="<?php echo randomNumber(4);?>" />
    PHP:
     
    jimbo2779, Jan 20, 2013 IP