Web Analytics Made Easy -
StatCounter rand( ) - CodingForum

Announcement

Collapse
No announcement yet.

rand( )

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • rand( )

    Hello,

    rand(1,2) gives me either 1 or 2.
    Is there a way to allow it to give 1.32, 1.55, ...
    Basically am looking for 2 decimals figures between 1 and 2 without having to go something like
    $x = rand(1,99);
    $y = 1.$x;

    Thanks,
    twomt
    Nemesis War
    Online browser based space combat game.
    It's free, it's great, you'll love it.
    Come join us at http://www.nemesiswar.net/

  • #2
    try something like this:
    PHP Code:
    rand(1.002.00
    if dosnt help, your way is the best
    Free php image upload script
    Personal web developing blog

    Comment


    • #3
      Originally posted by kreoton View Post
      try something like this:
      PHP Code:
      rand(1.002.00
      if dosnt help, your way is the best
      Nope, doesn't work. Gives me 1 or 2 only.

      I guess I'll have to stick to:

      $x = rand(1,99);
      $y = "1.$x";
      echo ($y);

      This gives me the required results. Thanks for the info anyway.
      Nemesis War
      Online browser based space combat game.
      It's free, it's great, you'll love it.
      Come join us at http://www.nemesiswar.net/

      Comment


      • #4
        couldnt you
        PHP Code:
        <?php
        $x 
        rand(100,200);

        $number $x;

        // english notation (default)
        $english_format_number number_format($number);
        Last edited by rafiki; Sep 29, 2006, 05:57 AM.

        Comment


        • #5
          Originally posted by rafiki View Post
          couldnt you
          PHP Code:
          <?php
          $x 
          rand(100,200);

          $number $x;

          // english notation (default)
          $english_format_number number_format($number);
          Yeah, but I would have to divide $x / 100 and so it is the same thing am doing now, just different way around. I was just hoping that rand(1.00,1.99) or something similar would have worked.
          Nemesis War
          Online browser based space combat game.
          It's free, it's great, you'll love it.
          Come join us at http://www.nemesiswar.net/

          Comment


          • #6
            rand()/mt_rand() only work with integers.
            PHP Code:
            <?php 
            function float_mt_rand($min$max

                for(
            $i 0; (int) $min != (float) $min || (int) $max != (float) $max; ++$i
                { 
                    
            $min *= 10
                    
            $max *= 10
                } 
                return 
            mt_rand($min$max) / pow(10$i); 

            var_dump(float_mt_rand(1.24493.1)); 
            ?>
            I'm not sure if this was any help, but I hope it didn't make you stupider.

            Experience is something you get just after you really need it.
            PHP Installation Guide Feedback welcome.

            Comment


            • #7
              sorry i no its an old thread but it seems such an easy answer
              PHP Code:
              <?php
              $x 
              rand(1,2);
              if (
              $x != 2) {
              $i rand(01,99);
              $x "$x.$i";
              echo 
              "$x";}
              else {echo 
              "$x.00";}
              ?>
              oh and heres a link keep refreshing it
              http://jay.stuff4yoursite.org/rand.php

              Comment

              Working...
              X