generate 2 random numbers in rapid sequence

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jim Michaels

    generate 2 random numbers in rapid sequence

    I need to generate 2 random numbers in rapid sequence from either PHP or
    mysql.
    I have not been able to do either. I get the same number back several times
    from PHP's mt_rand() and from mysql's RAND().
    any ideas?

    I suppose I could use the current rancom number as the seed for the next
    random number. but would that really work?


  • Domestos

    #2
    Re: generate 2 random numbers in rapid sequence

    People who work with computers often talk about their system's "random
    number generator" and the "random numbers" it produces. However numbers
    calculated by a computer through a deterministic process, cannot, by
    definition, be random. Given knowledge of the algorithm used to create the
    numbers and its internal state, you can predict all the numbers returned by
    subsequent calls to the algorithm, whereas with genuinely random numbers,
    knowledge of one number or an arbitrarily long sequence of numbers is of no
    use whatsoever in predicting the next number to be generated.

    Computer-generated "random" numbers are more properly referred to as
    pseudo-random numbers, and pseudo-random sequences of such numbers. A
    variety of clever algorithms have been developed which generate sequences of
    numbers which pass every statistical test used to distinguish random
    sequences from those containing some pattern or internal order.

    PHP has a number of inbuilt random functions such as rand(). This function
    must be first seeded by srand() in order for it to work properly.

    <?php

    // Range of numbers

    // Minimum number
    $min = "1";

    // Maximum number
    $max = "10";

    srand((double) microtime() * 1000003);
    // 1000003 is a prime number

    echo "rand() pseudo-random number".
    rand($min, $max);

    ?>


    Comment

    • Domestos

      #3
      Re: generate 2 random numbers in rapid sequence

      A less computationally expensive method of accomplishing the same thing is
      to use the mt_rand() function. This function uses the Mersenne Twister
      algorithm and is considerably faster than rand(). In addition as of PHP
      version 4.2.0 there is no need to seed the mt_rand() random number generator
      with srand() or mt_srand()

      <?php

      // Range of numbers

      // Minimum number
      $min = "1";

      // Maximum number
      $max = "10";

      echo "mt_rand() Mersenne Twister pseudo-random number: ".
      mt_rand($min, $max);

      ?>


      Comment

      • Jim Michaels

        #4
        Re: generate 2 random numbers in rapid sequence


        "Domestos" <andy.mak@virgi n.netspam> wrote in message
        news:1DA2g.2261 2$LH2.15753@new sfe2-win.ntli.net...[color=blue]
        >A less computationally expensive method of accomplishing the same thing is
        >to use the mt_rand() function. This function uses the Mersenne Twister
        >algorithm and is considerably faster than rand(). In addition as of PHP
        >version 4.2.0 there is no need to seed the mt_rand() random number
        >generator with srand() or mt_srand()
        >
        > <?php
        >
        > // Range of numbers
        >
        > // Minimum number
        > $min = "1";
        >
        > // Maximum number
        > $max = "10";
        >
        > echo "mt_rand() Mersenne Twister pseudo-random number: ".
        > mt_rand($min, $max);
        >
        > ?>
        >[/color]

        I know about these. that's why I mentioned them. my problem, as I stated,
        is that when I use them in an inline image generator to randomly select an
        image from a database, I get the same images on the page. I refresh, I get a
        different image because microtime has changed, but both images are again the
        same.

        This is supposed to be a random image picker. I don't even care if it's
        pseudo-random, as long as I get something that looks like a different image
        every time.

        any ideas?


        Comment

        • Jim Michaels

          #5
          Re: generate 2 random numbers in rapid sequence


          "Domestos" <andy.mak@virgi n.netspam> wrote in message
          news:1DA2g.2261 2$LH2.15753@new sfe2-win.ntli.net...[color=blue]
          >A less computationally expensive method of accomplishing the same thing is
          >to use the mt_rand() function. This function uses the Mersenne Twister
          >algorithm and is considerably faster than rand(). In addition as of PHP
          >version 4.2.0 there is no need to seed the mt_rand() random number
          >generator with srand() or mt_srand()
          >
          > <?php
          >
          > // Range of numbers
          >
          > // Minimum number
          > $min = "1";
          >
          > // Maximum number
          > $max = "10";
          >
          > echo "mt_rand() Mersenne Twister pseudo-random number: ".
          > mt_rand($min, $max);
          >
          > ?>
          >[/color]

          and by the way, I've tried PHP manual's suggested code
          <?php
          // seed with microseconds
          function make_seed()
          {
          list($usec, $sec) = explode(' ', microtime());
          return (float) $sec + ((float) $usec * 100000);
          }
          mt_srand(make_s eed());
          $randval = mt_rand();
          ?>

          but it doesn't make any difference. same result.

          I've also tried
          $q1=mysql_query ("SELECT MAX(pid) AS a FROM cpg133_pictures ", $link2);
          if ($row=mysql_fet ch_assoc($q1)) {
          time_nanosleep( 0, 100000);
          mt_srand(make_s eed()+$row['a']+rand());
          $n=mt_rand(1,$r ow['a']);


          but the 100ms nanosleep (which should affect microtime) didn't do anything
          either, neither does the mt_srand. maybe the nanosleep never did anything
          because it was too small for execution on UNIX.

          I have also tried feeding $n into mt_srand(). between calls, makes no
          difference. apparently mt_srand is set by default in PHP by microtime().
          so when the interpreter starts up for the 2nd session of my image generator,
          I'm stuck.

          I had thought I could at least get something decent out of mysql's RAND().


          Comment

          • Gordon Burditt

            #6
            Re: generate 2 random numbers in rapid sequence

            >I need to generate 2 random numbers in rapid sequence from either PHP or[color=blue]
            >mysql.[/color]

            Same page hit or different page hits? I cannot explain why you
            would get the same number in rapid sequence from several calls on
            the same hit.
            [color=blue]
            >I have not been able to do either. I get the same number back several times
            >from PHP's mt_rand() and from mysql's RAND().
            >any ideas?[/color]

            When PHP starts up, the seed gets initialized to <SOMETHING>,
            possibly based on microtime(), but I really don't care. Apache/PHP
            don't get restarted very often. With a good host, it could easily
            be less than once a month. However, once PHP starts, it's fixed.

            It is likely that if Apache fork()s for a given hit, the seed stays
            initialized to <SOMETHING> unless you explicitly set it. Any changes
            to that by more calls to get pseudo-random numbers are discarded
            when that instance of Apache exits. To get different values, you
            need to explicitly set the seed to a function of something other
            than just microtime().

            Things to use for a seed (jumble them all together, as in concatenate,
            then take md5 of result, then convert some of md5 result to integer):
            microtime()
            getmypid()
            $_SERVER['UNIQUE_ID'] (Apache only, and may need a module turned on)
            $_SERVER['REMOTE_ADDR']
            $_SERVER['REMOTE_PORT']

            Actually, using just the first two should be sufficient, as the
            pair (microtime(), getmypid()) should be unique. However, to get
            more entropy, throw in some of the others.

            If you are using pseudo-random numbers to select one of 100 images,
            expect the same image twice in a row about 1% of the time, unless
            you have code to deal with this. My suggestion is to not attempt
            to avoid this.
            [color=blue]
            >I suppose I could use the current rancom number as the seed for the next
            >random number. but would that really work?[/color]

            It's a very BAD idea to seed a pseudo-random number generator
            multiple times in the same run. Using the output of the random
            number generator as a new seed is also not a good idea. It's very
            easy to cripple a good pseudo-random number generator with a poor
            method of choosing a seed. The problem you're having with microtime()
            demonstrates this. Seed once. Use a good source of (pseudo-)randomness.
            microtime() alone doesn't qualify if you require rapid-fire results.

            Gordon L. Burditt

            Comment

            • Jim Michaels

              #7
              Re: generate 2 random numbers in rapid sequence


              "Gordon Burditt" <gordonb.cg3n5@ burditt.org> wrote in message
              news:124luplqsc mpm9f@corp.supe rnews.com...[color=blue][color=green]
              > >I need to generate 2 random numbers in rapid sequence from either PHP or
              >>mysql.[/color]
              >
              > Same page hit or different page hits? I cannot explain why you
              > would get the same number in rapid sequence from several calls on
              > the same hit.[/color]


              same page hit on the webserver to a .html file, which has 2 <img
              src=img.php> elements.
              on a multiprocessor webserver, this may be occurring simultaneously.

              [color=blue]
              >[color=green]
              >>I have not been able to do either. I get the same number back several
              >>times
              >>from PHP's mt_rand() and from mysql's RAND().
              >>any ideas?[/color]
              >
              > When PHP starts up, the seed gets initialized to <SOMETHING>,
              > possibly based on microtime(), but I really don't care. Apache/PHP
              > don't get restarted very often. With a good host, it could easily
              > be less than once a month. However, once PHP starts, it's fixed.
              >
              > It is likely that if Apache fork()s for a given hit, the seed stays
              > initialized to <SOMETHING> unless you explicitly set it. Any changes
              > to that by more calls to get pseudo-random numbers are discarded
              > when that instance of Apache exits. To get different values, you
              > need to explicitly set the seed to a function of something other
              > than just microtime().[/color]

              which makes sense. if all set to microtime, you would still get the same
              images.
              [color=blue]
              >
              > Things to use for a seed (jumble them all together, as in concatenate,
              > then take md5 of result, then convert some of md5 result to integer):
              > microtime()
              > getmypid()[/color]

              believe it or not, on these 2 I get the same pictures. frustrating.
              [color=blue]
              > $_SERVER['UNIQUE_ID'] (Apache only, and may need a module turned on)[/color]

              Don't think I have that option. sure sounds good though.
              [color=blue]
              > $_SERVER['REMOTE_ADDR']
              > $_SERVER['REMOTE_PORT'][/color]

              these two won't make it unique, because both images are going to be all on
              the same page.
              mt_srand(make_s eed()+getmypid( )+$_SERVER['UNIQUE_ID']);
              $n=mt_rand(1,$r ow['a']);
              tried this, but still doesn't do it. I don't even get an error on
              $_SERVER['UNIQUE_ID']. I think it's NULL. is $_SERVER['UNIQUE_ID'] a
              string I should hash, or an integer?
              [color=blue]
              >
              > Actually, using just the first two should be sufficient, as the
              > pair (microtime(), getmypid()) should be unique. However, to get
              > more entropy, throw in some of the others.
              >
              > If you are using pseudo-random numbers to select one of 100 images,
              > expect the same image twice in a row about 1% of the time, unless
              > you have code to deal with this. My suggestion is to not attempt
              > to avoid this.
              >[/color]

              I get a different image on next page refresh, possibly due to the time
              involved. but the same images all over the page, possibly due to the time
              involved.
              [color=blue][color=green]
              >>I suppose I could use the current rancom number as the seed for the next
              >>random number. but would that really work?[/color]
              >
              > It's a very BAD idea to seed a pseudo-random number generator
              > multiple times in the same run. Using the output of the random
              > number generator as a new seed is also not a good idea. It's very
              > easy to cripple a good pseudo-random number generator with a poor
              > method of choosing a seed. The problem you're having with microtime()[/color]

              I understand that. I was trying to avoid it. but nothing seems to work
              short of having semaphore'd access to a file or database as the seed and
              just incrementing it like a counter. And then I'm circumventing the web
              server's whole concept of parallelism. :-/
              [color=blue]
              > demonstrates this. Seed once. Use a good source of (pseudo-)randomness.
              > microtime() alone doesn't qualify if you require rapid-fire results.
              >[/color]

              tell me about it. :-/
              [color=blue]
              > Gordon L. Burditt[/color]


              Comment

              • Gordon Burditt

                #8
                Re: generate 2 random numbers in rapid sequence

                >> >I need to generate 2 random numbers in rapid sequence from either PHP or[color=blue][color=green][color=darkred]
                >>>mysql.[/color]
                >>
                >> Same page hit or different page hits? I cannot explain why you
                >> would get the same number in rapid sequence from several calls on
                >> the same hit.[/color]
                >
                >
                >same page hit on the webserver to a .html file, which has 2 <img
                >src=img.php> elements.
                >on a multiprocessor webserver, this may be occurring simultaneously.[/color]

                Show the code for generating the two <img src= tags. Since PHP is
                a procedural and uni-tasking (within any page, unless you start
                calling fork()) language, these should NOT be done simultaneously,
                even on a multi-processor system, although they might be done fast
                enough that microtime() doesn't advance. Also show all the calls to get
                random numbers and set seeds.

                Are you sure you're not seeding twice with the same seed? SEED ONLY ONCE.

                Look at the source HTML code. Are you getting:

                - the same image number (e.g. 1) all the time?
                - random first image number but the second is always the same as the first?
                [color=blue][color=green]
                >> Things to use for a seed (jumble them all together, as in concatenate,
                >> then take md5 of result, then convert some of md5 result to integer):
                >> microtime()
                >> getmypid()[/color]
                >
                >believe it or not, on these 2 I get the same pictures. frustrating.[/color]

                Are you running Apache 2.0? It may be using threads instead of
                processes, even for PHP. Is PHP even supposed to work with Apache
                2.0 yet? On a multiprocessor system?
                [color=blue][color=green]
                >> $_SERVER['UNIQUE_ID'] (Apache only, and may need a module turned on)[/color]
                >
                >Don't think I have that option. sure sounds good though.[/color]

                The module name is mod_unique_id.
                [color=blue]
                >[color=green]
                >> $_SERVER['REMOTE_ADDR']
                >> $_SERVER['REMOTE_PORT'][/color]
                >
                >these two won't make it unique, because both images are going to be all on
                >the same page.
                > mt_srand(make_s eed()+getmypid( )+$_SERVER['UNIQUE_ID']);
                > $n=mt_rand(1,$r ow['a']);[/color]

                Show me the code to generate the SECOND id. You don't repeat both
                of those lines of code, do you? SEED ONLY ONCE!
                [color=blue]
                >tried this, but still doesn't do it. I don't even get an error on
                >$_SERVER['UNIQUE_ID']. I think it's NULL. is $_SERVER['UNIQUE_ID'] a
                >string I should hash, or an integer?[/color]

                It's a string. Try printing it just to see if it's getting set at all.
                If it is getting set, it's probably being interpreted as the integer 0
                because you're trying to use it as a number.

                Please verify your code:

                1) There is at most one call to microtime() in any of the files used
                by this page. This call is NOT inside a function. More specifically,
                this call is NOT inside make_seed(). Delete that function and
                expand it in line (ONCE) if desired.
                2) There is at most one call to any function to set a seed (srand,
                mt_srand). This call is NOT inside a function.

                Gordon L. Burditt

                Comment

                • Jim Michaels

                  #9
                  Re: generate 2 random numbers in rapid sequence


                  "Gordon Burditt" <gordonb.cg3n5@ burditt.org> wrote in message
                  news:124luplqsc mpm9f@corp.supe rnews.com...[color=blue][color=green]
                  > >I need to generate 2 random numbers in rapid sequence from either PHP or
                  >>mysql.[/color]
                  >
                  > Same page hit or different page hits? I cannot explain why you
                  > would get the same number in rapid sequence from several calls on
                  > the same hit.
                  >[color=green]
                  >>I have not been able to do either. I get the same number back several
                  >>times
                  >>from PHP's mt_rand() and from mysql's RAND().
                  >>any ideas?[/color]
                  >
                  > When PHP starts up, the seed gets initialized to <SOMETHING>,
                  > possibly based on microtime(), but I really don't care. Apache/PHP
                  > don't get restarted very often. With a good host, it could easily
                  > be less than once a month. However, once PHP starts, it's fixed.
                  >
                  > It is likely that if Apache fork()s for a given hit, the seed stays
                  > initialized to <SOMETHING> unless you explicitly set it. Any changes
                  > to that by more calls to get pseudo-random numbers are discarded
                  > when that instance of Apache exits. To get different values, you
                  > need to explicitly set the seed to a function of something other
                  > than just microtime().
                  >
                  > Things to use for a seed (jumble them all together, as in concatenate,
                  > then take md5 of result, then convert some of md5 result to integer):
                  > microtime()
                  > getmypid()
                  > $_SERVER['UNIQUE_ID'] (Apache only, and may need a module turned on)
                  > $_SERVER['REMOTE_ADDR']
                  > $_SERVER['REMOTE_PORT']
                  >
                  > Actually, using just the first two should be sufficient, as the
                  > pair (microtime(), getmypid()) should be unique. However, to get
                  > more entropy, throw in some of the others.
                  >
                  > If you are using pseudo-random numbers to select one of 100 images,
                  > expect the same image twice in a row about 1% of the time, unless
                  > you have code to deal with this. My suggestion is to not attempt
                  > to avoid this.
                  >[color=green]
                  >>I suppose I could use the current rancom number as the seed for the next
                  >>random number. but would that really work?[/color]
                  >
                  > It's a very BAD idea to seed a pseudo-random number generator
                  > multiple times in the same run. Using the output of the random
                  > number generator as a new seed is also not a good idea. It's very
                  > easy to cripple a good pseudo-random number generator with a poor
                  > method of choosing a seed. The problem you're having with microtime()
                  > demonstrates this. Seed once. Use a good source of (pseudo-)randomness.
                  > microtime() alone doesn't qualify if you require rapid-fire results.
                  >
                  > Gordon L. Burditt[/color]

                  well, I have one last thing I can do. I asked about doing XML inline images
                  and got this really cook info. hope it would work with HTML.
                  echo "<img src=\"data:$mim etype;base64," .
                  base64_encode(f ile_get_content s($filepath)) . "\" alt=\"$alt\" width=\"$w\"
                  height=\"$h\" style=\"$style\ "$end>";

                  just insert this into a function. from http://www.ietf.org/rfc/rfc2397.txt .
                  calling a function to generate an imline image should allow me to use the
                  random number generator as it's supposed to be used. then I don't have to
                  use <img src="img.php">

                  just tried it. output looks correct, but doesn't show an image. :-(
                  and base64_encode() sure is slow!
                  Alas, I found out this only works in NS, not IE. IE requires MIME encoding,
                  which I've never done, and since it looks like it requires headers, I may be
                  back to square one problem with the seed.


                  Comment

                  • Geoff Berrow

                    #10
                    Re: generate 2 random numbers in rapid sequence

                    Message-ID: <rfmdnWoymrRuqN bZnZ2dnUVZ_tedn Z2d@comcast.com > from Jim
                    Michaels contained the following:
                    [color=blue]
                    >well, I have one last thing I can do.[/color]

                    More than that. I've used rand() loads of times and not had a problem.

                    See:http://www.ckdog.co.uk/phpcourse/DICE/DICEGAME.PHP which picks two
                    random numbers and displays images according to what the number is.

                    With the code

                    $num=rand(1,6);
                    $num2=rand(1,6) ;
                    $total=$num+$nu m2;

                    $num1 and $num2 are only occasionally the same (as you might expect)


                    Full code here:-


                    --
                    Geoff Berrow (put thecat out to email)
                    It's only Usenet, no one dies.
                    My opinions, not the committee's, mine.
                    Simple RFDs http://www.ckdog.co.uk/rfdmaker/

                    Comment

                    • Jim Michaels

                      #11
                      Re: generate 2 random numbers in rapid sequence


                      "Gordon Burditt" <gordonb.a2nrk@ burditt.org> wrote in message
                      news:124mac1o4m f8f6d@corp.supe rnews.com...[color=blue][color=green][color=darkred]
                      >>> >I need to generate 2 random numbers in rapid sequence from either PHP
                      >>> >or
                      >>>>mysql.
                      >>>
                      >>> Same page hit or different page hits? I cannot explain why you
                      >>> would get the same number in rapid sequence from several calls on
                      >>> the same hit.[/color]
                      >>
                      >>
                      >>same page hit on the webserver to a .html file, which has 2 <img
                      >>src=img.php > elements.
                      >>on a multiprocessor webserver, this may be occurring simultaneously.[/color]
                      >[/color]

                      this is essentially what I had before I switched over to something that I
                      found out later only works on NS :-/

                      <?php
                      function makeThumbnail($ o_file, /*$t_ht = 150*/$t_wd=150) {
                      $image_info = getimagesize($o _file); // see EXIF for faster way

                      switch ($image_info['mime']) {
                      case 'image/gif':
                      if (imagetypes() & IMG_GIF) { // not the same as IMAGETYPE
                      $o_im = imagecreatefrom gif($o_file) ;
                      } else {
                      $ermsg = 'GIF images are not supported<br />';
                      }
                      break;
                      case 'image/jpeg':
                      if (imagetypes() & IMG_JPG) {
                      $o_im = imagecreatefrom jpeg($o_file) ;
                      } else {
                      $ermsg = 'JPEG images are not supported<br />';
                      }
                      break;
                      case 'image/png':
                      if (imagetypes() & IMG_PNG) {
                      $o_im = imagecreatefrom png($o_file) ;
                      } else {
                      $ermsg = 'PNG images are not supported<br />';
                      }
                      break;
                      case 'image/wbmp':
                      if (imagetypes() & IMG_WBMP) {
                      $o_im = imagecreatefrom wbmp($o_file) ;
                      } else {
                      $ermsg = 'WBMP images are not supported<br />';
                      }
                      break;
                      default:
                      $ermsg = $image_info['mime'].' images are not supported<br />';
                      break;
                      }

                      if (!isset($ermsg) ) {
                      $o_wd = imagesx($o_im) ;
                      $o_ht = imagesy($o_im) ;
                      // thumbnail width = target * original width / original height
                      //$t_wd = round($o_wd * $t_ht / $o_ht) ;
                      $t_ht = round($o_ht * $t_wd / $o_wd);
                      $t_im = imagecreatetrue color($t_wd,$t_ ht);

                      imagecopyresamp led($t_im, $o_im, 0, 0, 0, 0, $t_wd, $t_ht, $o_wd,
                      $o_ht);

                      imagejpeg($t_im ); //output to browser

                      imagedestroy($o _im);
                      imagedestroy($t _im);
                      }
                      return isset($ermsg)?$ ermsg:NULL;
                      }

                      function make_seed()
                      {
                      list($usec, $sec) = explode(' ', microtime());
                      return (float) $sec + ((float) $usec * 100000);
                      }

                      //get next row from pictures table since we can't do random
                      $q2=mysql_query ("SELECT pictures.pid AS pid
                      FROM pictures,idx
                      WHERE pictures.pid>id x.pid
                      LIMIT 1", $link2);
                      if ($row2=mysql_fe tch_assoc($q2)) {
                      mysql_query("UP DATE idx SET pid=$row2[pid]", $link2);
                      $n=$row2['pid'];
                      } else { //reached end of table. wrap to beginning.
                      mysql_query("UP DATE idx SET pid=1", $link2);
                      $n=1;
                      }
                      mysql_free_resu lt($q2);

                      //try random anyway. overwrite results of "nextrow".
                      $q3=mysql_query ("SELECT MAX(pid) AS a FROM cpg133_pictures ", $link2);
                      if ($row=mysql_fet ch_assoc($q3)) {
                      //tried commenting out the line below. no difference.
                      mt_srand(make_s eed()+getmypid( )/*+$_SERVER['UNIQUE_ID']*/);
                      $n=mt_rand(1,$r ow['a']);
                      //$n=rand(1,$row['a']); //for some reason, causes code to break
                      }
                      mysql_free_resu lt($q3);
                      $result = mysql_query("SE LECT filepath,filena me,pwidth,pheig ht FROM pictures
                      WHERE pid=$n", $link2);
                      if ($row = mysql_fetch_ass oc($result)) {
                      //determine mime type from extension on filename
                      $ext=strrchr($r ow['filename'],'.');
                      switch($ext){
                      case '.jp2':
                      case '.jpg':
                      case '.jpeg':
                      default: $mimetype='imag e/jpeg';break;
                      case '.gif': $mimetype='imag e/gif';break;
                      case '.png': $mimetype='imag e/x-png';break;
                      }
                      $fname=$_SERVER['DOCUMENT_ROOT'] . '/pix/' . $row['filepath'] .
                      $row['filename'];
                      }
                      mysql_free_resu lt($result);
                      mysql_close($li nk2);

                      if (isset($_GET['width'])) {
                      makeThumbnail($ fname, $_GET['width']);
                      } else {
                      makeThumbnail($ fname);
                      }
                      ?>
                      //example: <img src="img.php?wi dth=150" width=150>

                      [color=blue]
                      > Show the code for generating the two <img src= tags. Since PHP is
                      > a procedural and uni-tasking (within any page, unless you start
                      > calling fork()) language, these should NOT be done simultaneously,
                      > even on a multi-processor system, although they might be done fast
                      > enough that microtime() doesn't advance. Also show all the calls to get
                      > random numbers and set seeds.
                      >[/color]

                      IE requests/loads images in series-parallel very rapidly. Have you ever
                      watched it? That's where it's coming from I think.

                      [color=blue]
                      > Are you sure you're not seeding twice with the same seed? SEED ONLY ONCE.
                      >
                      > Look at the source HTML code. Are you getting:
                      >
                      > - the same image number (e.g. 1) all the time?
                      > - random first image number but the second is always the same as the
                      > first?
                      >[color=green][color=darkred]
                      >>> Things to use for a seed (jumble them all together, as in concatenate,
                      >>> then take md5 of result, then convert some of md5 result to integer):
                      >>> microtime()
                      >>> getmypid()[/color]
                      >>
                      >>believe it or not, on these 2 I get the same pictures. frustrating.[/color]
                      >
                      > Are you running Apache 2.0? It may be using threads instead of
                      > processes, even for PHP. Is PHP even supposed to work with Apache
                      > 2.0 yet? On a multiprocessor system?
                      >[color=green][color=darkred]
                      >>> $_SERVER['UNIQUE_ID'] (Apache only, and may need a module turned on)[/color]
                      >>
                      >>Don't think I have that option. sure sounds good though.[/color]
                      >
                      > The module name is mod_unique_id.
                      >[color=green]
                      >>[color=darkred]
                      >>> $_SERVER['REMOTE_ADDR']
                      >>> $_SERVER['REMOTE_PORT'][/color]
                      >>
                      >>these two won't make it unique, because both images are going to be all on
                      >>the same page.
                      >> mt_srand(make_s eed()+getmypid( )+$_SERVER['UNIQUE_ID']);
                      >> $n=mt_rand(1,$r ow['a']);[/color]
                      >
                      > Show me the code to generate the SECOND id. You don't repeat both
                      > of those lines of code, do you? SEED ONLY ONCE!
                      >[color=green]
                      >>tried this, but still doesn't do it. I don't even get an error on
                      >>$_SERVER['UNIQUE_ID']. I think it's NULL. is $_SERVER['UNIQUE_ID'] a
                      >>string I should hash, or an integer?[/color]
                      >
                      > It's a string. Try printing it just to see if it's getting set at all.
                      > If it is getting set, it's probably being interpreted as the integer 0
                      > because you're trying to use it as a number.
                      >
                      > Please verify your code:
                      >
                      > 1) There is at most one call to microtime() in any of the files used
                      > by this page. This call is NOT inside a function. More specifically,
                      > this call is NOT inside make_seed(). Delete that function and
                      > expand it in line (ONCE) if desired.
                      > 2) There is at most one call to any function to set a seed (srand,
                      > mt_srand). This call is NOT inside a function.
                      >
                      > Gordon L. Burditt[/color]


                      Comment

                      • fletch

                        #12
                        Re: generate 2 random numbers in rapid sequence

                        Well, don't seed based on time. Use getmypid() as part of the seeding.
                        Perhaps you could get the two processes aware of each other, or aware
                        of the last choice made.

                        Comment

                        • Jim Michaels

                          #13
                          Re: generate 2 random numbers in rapid sequence


                          "fletch" <richard.a.flet cher@googlemail .com> wrote in message
                          news:1145865073 .709693.178550@ i40g2000cwc.goo glegroups.com.. .[color=blue]
                          > Well, don't seed based on time. Use getmypid() as part of the seeding.
                          > Perhaps you could get the two processes aware of each other, or aware
                          > of the last choice made.
                          >[/color]

                          found out that the database is an excellent source of random numbers if you
                          are stuck doing <img src=img.php...>

                          Also discovered that my host's apache configuration might be load PHP as an
                          ..SO module under UNIX, therefore PHP *might* not get its own PID. If UNIX
                          ..SO's are anything like Windows DLL's, only Apache has the PID - PHP
                          wouldn't even be a child process - it is simply allocated a chunk of memory
                          for its functions to load in.

                          If this were a CGI, then it would definitely get its own PID, but PHP.net
                          doesn't recommend this method of configuration.

                          To be honest, I haven't tried getmypid() after discovering that the URL on
                          src= needs to be unique so that the image isn't cached and therefore
                          duplicated. Now that I've got everything working, maybe I'll give it one
                          more go just to find out.


                          Comment

                          Working...