Another random number question

Discussion in 'PHP' started by enchance, Nov 21, 2008.

  1. #1
    Let's say $mynum = 3. What function can give me a random number from 1-99 except the current value of $mynum? Noob here (obviously).
     
    enchance, Nov 21, 2008 IP
  2. sandstorm140

    sandstorm140 Peon

    Messages:
    80
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    <?php
    
    $mynum = rand(1,99);
    echo $mynum;
    
    ?>
    
    PHP:
     
    sandstorm140, Nov 21, 2008 IP
  3. AdultProfiles

    AdultProfiles Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Or something like this:

    $num = rand(1,99);
    while($num == $mynum){
    $num = rand(1,99);
    }
    $mynum = $num;

    echo $mynum.


    Other option is to use an array: $arr = range(1,99); $mynum = $arr[rand(1,99)];
     
    AdultProfiles, Nov 21, 2008 IP
  4. atlantaazfinest

    atlantaazfinest Peon

    Messages:
    389
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #4
    
    <?php
    $mynum = 3;
    $rand =  rand(1,99);
    if($rand == $mynum)
    {
    $rand = rand(1,99);
    }
    
    echo $rand;
    ?>
    
    Code (markup):
    that should be what you want
     
    atlantaazfinest, Nov 21, 2008 IP
  5. AdultProfiles

    AdultProfiles Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    My mistake: while($num == $mynum)
     
    AdultProfiles, Nov 21, 2008 IP