I want to give it a range (ie 1-5) and have it randomely select a number within it. How would I go about doing that?

TIA 😃

    Try the following. I simply use the PID as an initial random number generator, then grab part of it and compare it to the min and max values.

    <?php

                // Set your min and max values here
    $min = 1;
    $max = 5;
    
                // Get the process ID number
    $random = getmypid();
    
                // Grab part of the ID number. I start at the second
                // character since  the first character is often '1'. I also
                // grab enough characters (i.e. if my max number is a
                // double digit number, I grab 2 characters from the
                // $random string.
    $first = substr($random,1,strlen($max));
    
                // If the number I grab is larger than the 'max' value,
                // I simply subtract the max value from it. If the number
                // I grab is smaller than the min value, I add the 'max' 
                // value.   
    if ($first > $max) {
    	$first = $first - $max;
    } elseif ($first < $min) {
    	$first = $first + $max;
    }
    
                //Now remove leading zeros.
    $first = ltrim($first, '0');
    
    echo "Random Number is $first";

    ?>

      Originally posted by mutchman
      Try the following. I simply use the PID as an initial random number generator, then grab part of it and compare it to the min and max values.

      <?php

                  // Set your min and max values here
      $min = 1;
      $max = 5;
      
                  // Get the process ID number
      $random = getmypid();
      
                  // Grab part of the ID number. I start at the second
                  // character since  the first character is often '1'. I also
                  // grab enough characters (i.e. if my max number is a
                  // double digit number, I grab 2 characters from the
                  // $random string.
      $first = substr($random,1,strlen($max));
      
                  // If the number I grab is larger than the 'max' value,
                  // I simply subtract the max value from it. If the number
                  // I grab is smaller than the min value, I add the 'max' 
                  // value.   
      if ($first > $max) {
      	$first = $first - $max;
      } elseif ($first < $min) {
      	$first = $first + $max;
      }
      
                  //Now remove leading zeros.
      $first = ltrim($first, '0');
      
      echo "Random Number is $first";

      ?> [/B]

      Thanks man but it seems to be stuck on 4 for some reason. Every time I reload the page is says: Random Number is 4.

        I ran it from the command line and had great results. Not sure if what you are doing is a caching problem or something else.

          Originally posted by mutchman
          I ran it from the command line and had great results. Not sure if what you are doing is a caching problem or something else.

          I think it may be a caching issue because I tried to do what I want using MySQL's:

          select * from table order by rand() limit 1;

          It works great when I run a query within mysql but when I use the it in a PHP page it selects the same # every time.

          I'll figure it out.

          Thanks again 😃

            I found something simpler and it actually works! 😃

            <?php
            $num = 5;
            $generate = rand(1,$num);
            echo ($generate);
            ?>
            
              Write a Reply...