Php and Random numbers

Discussion in 'PHP' started by Dirty-Rockstar, Feb 14, 2007.

  1. #1
    Extremely new to php. Just started learning 3 days go :) im trying to print a random number from one to ten. would this work? or should i use echo? or am i totally in left field here. thanks. would it be pure random? like a 1 in 10 chance for each number to show?

    
    <?
    
    $Number=rand(1,10)
    
    print("$Number");
    
    ?>
    
    
    Code (markup):
    Edit: wrong opening tag
     
    Dirty-Rockstar, Feb 14, 2007 IP
  2. Munk

    Munk Peon

    Messages:
    56
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If you want to get in to programming, get familiar with and used to looking at programmer's documentation. In this case, http://www.php.net/manual/en/
     
    Munk, Feb 14, 2007 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    Print and echo works... But not in your case, cause variables are case sensitive.
     
    nico_swd, Feb 14, 2007 IP
  4. Dirty-Rockstar

    Dirty-Rockstar Guest

    Messages:
    252
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #4

    yeah, sorry i knew that. ill edit and change it. and for the first poster thanks for that link as well. im reading a how to book by sams. for mysql apache and php.

    :) so i guess with the change it will work :) thanks
     
    Dirty-Rockstar, Feb 14, 2007 IP
  5. liamvictor

    liamvictor Peon

    Messages:
    127
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #5
    int rand ( [int $min, int $max] )

    If called without the optional min, max arguments rand() returns a pseudo-random integer between 0 and RAND_MAX. If you want a random number between 5 and 15 (inclusive), for example, use rand (5, 15).

    In general, rand is good enough.

    Pseudo-random PHP functions and truly random number generators - see http://www.phpfive.net/article2.htm.

    In your example, you'd need:
    
    $Number=rand(1,10)
    print("$Number");
    
    Code (markup):
     
    liamvictor, Feb 14, 2007 IP
  6. BenjArriola

    BenjArriola Peon

    Messages:
    175
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I'm not going to talk about what is good or not on how to implement this. This is just a simple random number from 1 to 10.

    Strictly speaking, this will not work just in case you were testing it:

    
    $Number=rand(1,10)
    print("$Number");
    
    PHP:
    Simply because you have no semicolon at the end of the first line. This should work.

    
    $Number=rand(1,10);
    print("$Number");
    
    PHP:
     
    BenjArriola, Feb 14, 2007 IP
  7. bobby9101

    bobby9101 Peon

    Messages:
    3,292
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    0
    #7
    no it would work, because his variable is with an uppcase and the print has an uppercase
     
    bobby9101, Feb 15, 2007 IP
  8. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #8
    He edited his post. The cases were different when I posted my comment.

     
    nico_swd, Feb 15, 2007 IP
  9. bobby9101

    bobby9101 Peon

    Messages:
    3,292
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    0
    #9
    ahh ok, good eyes then... I am a all lower caps guy, so camel caps annoys me because I can never pick it up
     
    bobby9101, Feb 15, 2007 IP