I want to find a way to generate some random IP Addresses.
Just for testing my user database.

The function below works as far I can see.
But I wonder if there is some shorter, smart way to do this?

I am sure it is 😕

<?php

function make_ip(){
    $inum1 = rand(0,255);
    $inum2 = rand(0,255);
    $inum3 = rand(0,255);
    $inum4 = rand(0,255);
    $ip = $inum1.'.'.$inum2.'.'.$inum3.'.'.$inum4;
    return $ip;    
} echo make_ip(); ?>
    echo long2ip(rand(0, 255*255)*rand(0, 255*255));
    

      Thanks for your suggestion.
      You got my brain to start thinking.

      What we need is a random value between
      0 - (256256256*256 -1) which is 232 -1

      Problem with rand is that in my PC randmax = 32767 (= 128x256-1).
      But the function [man]mt_rand[/man] has [man]mt_getrandmax[/man]
      a max of: 2147483647 = 231 -1

      Much to my surprise using you suggestion and mt_rand()
      this is possible to solve!
      Because 66537 65535 = 232 -1 which makes IP 255.255.255.255
      In other words: b
      (216-1)[/b]

      Here is my solution

      <?php
      
      echo long2ip(256*256*256*256-1);// 255.255.255.255
      echo '<br>';
      echo long2ip(256*256*256*256-2);// 255.255.225.254
      echo '<br>';
      echo 256*256*256*256-1;   // 4294967295
      echo '<br>';
      echo 65537 * 65535;       // 4294967295
      echo '<br>';
      echo 'r '.getrandmax();   // 32767
      echo '<br>';
      echo 'm '.mt_getrandmax();// 2147483647
      echo '<br>';
      echo '<br>';
      
      echo long2ip( mt_rand(0, 65537) * mt_rand(0, 65535) );
      
      ?>

        so far we have 3suggestions
        how to make a random IP Address from 0.0.0.0 - 255.255.255.255

        1. My Function

        2. NogDog suggestion
          ( Think it will not work properly. Because rand() max and because it anyway does not produce the higher numbers, upto 2564 -1

        3. My new suggestion based on NogDog idea, but using mt_rand to produce from 0 to 2564 -1

        Any new ideas?? 🙂

        Here is code.

        My version 1

        <?php 
        
        function make_ip(){ 
            $inum1 = rand(0,255); 
            $inum2 = rand(0,255); 
            $inum3 = rand(0,255); 
            $inum4 = rand(0,255); 
            $ip = $inum1.'.'.$inum2.'.'.$inum3.'.'.$inum4; 
            return $ip;     
        }

        Nogdog version 2

        <?php
        
        echo long2ip(rand(0, 255*255) * rand(0, 255*255));

        My final version 3

        <?php
        
        echo long2ip( mt_rand(0, 65537) * mt_rand(0, 65535) );
        
        ?>

          You need to be careful of your ranges. 66537 * 65535 = 232 -1, but note that these numbers denote the size of the ranges. [0,66537] has 66537 possible integer values, while [0,65535] has 65536 possible values. As such, your current function produces values that are outside of the range of long2ip().

            If I wanted to make a more generally useful function where I could optionally control the type of network address, I might use the following (even if it is more verbose and possibly takes a couple more microseconds to process):

            /**
            * generate a random IP address
            *
            * @return string
            * @param string $type  (optional) IP address class: A, B, C, or Local (case-insensitive
            */
            function random_ip($type = null)
            {
               $type = ($type != null) ? strtoupper($type) : $type;
               switch($type)
               {
                  case 'A':
                     $min = 1;
                     $max = 126;
                     break;
                  case 'B':
                     $min = 128;
                     $max = 191;
                     break;
                  case 'C':
                     $min = 192;
                     $max = 223;
                     break;
                  case 'LOCAL':
                     $min = 127;
                     $max = 127;
                     break;
                  default:
                     $min = 1;
                     $max = 255;
               }
               $ip = sprintf('%03d', rand($min, $max));
               for($i = 0; $i < 3; $i++)
               {
                  $ip .= sprintf('.%03d', rand(0, 255));
               }
               return($ip);
            }
            
              laserlight wrote:

              As such, your current function produces values that are outside of the range of long2ip().

              You are wrong.

              And if you would try my code with comments
              ( http://phpbuilder.com/board/showpost.php?p=10830892&postcount=3 )
              you would see that my one-liner works.
              I did my investigation with many attempts and trials.

              It produces 0.0.0.0 - 255.255.255.255 with [man]long2ip[/man]

              I wouldnt publish my code, if I wasnt sure it worked.
              I will not explain once again,
              instead I recommend read / testrun of my previous post:

              http://phpbuilder.com/board/showpost.php?p=10830892&postcount=3

              /tinder

                NogDog wrote:

                If I wanted to make a more generally useful function where I could optionally control the type of network address, ...

                I understand.
                Yes, you can use that function. If you need it.

                But my question is only a smartest/shortest way
                to generate any possible IP ( todays standard 0.0.0.0 ).
                And best of course if is a one-liner.

                NogDog.
                You havent told what (is wrong) about my version 3.
                Maybe you do not want to????
                Because you did not make it maybe 😃 😃

                  Ease up, dude. I never said anything was wrong, did I? I just provided another alternative. The shortest/fastest code is not always the best solution. Sometimes it is, but sometimes the best solution is the one that is more portable, scalable, and easier to modify and maintain by others who may inherit your code. Therefore neither is "right" or "wrong"; you need to decide what is important to you for your particular situation. The shortest code is not always the best, especially when you are trying to maintain someone else's code -- or even worse: you are trying to modify code you wrote a year or two ago and find yourself wondering, "W.t.f. was I doing when I wrote this?" 😉 Then again, if you are designing a real-time missile guidance system where every nanosecond is important, you may have to sacrifice ease of maintenance for the fastest and most efficient code you can come up with.

                    tinder wrote:

                    You are wrong.

                    And if you would try my code with comments
                    ( http://phpbuilder.com/board/showpost.php?p=10830892&postcount=3 )
                    you would see that my one-liner works.
                    I did my investigation with many attempts and trials.

                    It produces 0.0.0.0 - 255.255.255.255 with [man]long2ip[/man]

                    I wouldnt publish my code, if I wasnt sure it worked.
                    I will not explain once again,
                    instead I recommend read / testrun of my previous post:

                    http://phpbuilder.com/board/showpost.php?p=10830892&postcount=3

                    /tinder

                    Sorry, I overlooked the underlying issue myself. Must have been too tired last night 🙂

                    Incidentally, merely testing it as is will not guarantee that it works since the PRNG might not generate the edge cases that may make it fail. Consequently, an "investigation with many attempts and trials" is not enough.

                    However, now that I have come back to this thread to check, I see that indeed your calculations are correct since it is the maximum bound that is in question, not the size of the range of the mt_rand() call.

                    The problem as it stands (which NogDog has solved, I think) is that you may generate IP addresses that are not suitable/realistic for testing. A quick fix for this is to check the generated number against a list of unsuitable IP addresses (e.g., 0), and if there is a match, generate a new number.

                      Hi all,

                      Yes whereas Tinder's short 1 liners might be just a wee bit faster and as laserlight pointed out it has a possibility of returning nonsence IP ranges.

                      NogDog's example is the best to go with as it offers you the option to call IP's within a range of your liking/need.

                      A shortened and less diverse version of NogDog's example would be

                      function randomip()
                      {
                         $rand = array();
                         for($i = 0; $i < 4; $i++) 
                         { 
                            $rand[] = rand(1,255); 
                         } 
                         $ip = "$rand[0].$rand[1].$rand[2].$rand[3]";
                         return($ip);
                      }
                      

                      Correct me if i'm wrong?

                        Waptech, your function seems like it's fairly solid - it eliminates the issue of out-of-bounds numbers, and wouldn't return something like 782.659.145.200.

                        And just a note, instead of using [ code ] tags, you could use [ php ] tags and get the syntax highlighting.

                          Also, from php.net/long2ip's comments:

                          Random ip address! (4294967295 is 255.255.255.255)

                          long2ip(rand(0, "4294967295"))

                          Edit: I tested this just for kicks, in a for < 100 loop, and some of the sample results are:

                          220.50.73.4
                          141.140.133.13
                          187.128.105.214
                          250.234.86.209
                          202.85.242.48
                          169.24.136.237
                          232.145.187.154
                          130.168.220.225
                          145.230.30.35
                          253.230.34.76
                          172.143.97.166
                          189.78.146.16
                          146.134.122.31
                          196.62.31.150
                          148.174.83.78
                          153.245.105.29
                          153.52.25.175

                          So it doesn't seem to generate anything wild - would work fine for simple log-generations or something for testing a parsing script or something like that.

                            Write a Reply...