Not generating the same random number twice....

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ewoud Dronkert

    #16
    Re: Not generating the same random number twice....

    On Tue, 14 Jun 2005 08:35:44 GMT, nemo wrote:[color=blue]
    > All these much more sophisticated solutions make me wonder where I went
    > wrong in the early days.
    > $Query="select image_filename from Tablename order by rand() limit 5";[/color]

    1. His image filenames are not in a db :)
    2. Order by rand potentially causes enormous overhead. See for example
    http://jan.kneschke.de/projects/mysql/order-by-rand/ (performance data
    at bottom of page).


    --
    Firefox Web Browser - Rediscover the web - http://getffox.com/
    Thunderbird E-mail and Newsgroups - http://gettbird.com/

    Comment

    • Geoff Berrow

      #17
      Re: Not generating the same random number twice....

      I noticed that Message-ID: <av4ta1h08up98h ikop6q8jldgsiho bqkpn@4ax.com>
      from Ewoud Dronkert contained the following:
      [color=blue]
      >On Mon, 13 Jun 2005 23:54:08 +0100, Geoff Berrow wrote:[color=green]
      >> for($i=1;$i<50; $i++){
      >> $balls[]=$i;
      >> }[/color]
      >
      >See http://php.net/range[/color]

      Code is from a course I run and it's a good idea to get the students
      used to using loops[color=blue]
      >[color=green]
      >> for($i=0;$i<6;$ i++){
      >> $key=array_rand ($balls);
      >> $ballschosen[]=$balls[$key];
      >> unset($balls[$key]);
      >> }[/color]
      >
      >See http://php.net/shuffle[/color]

      For some reason I've never been able to get shuffle to work properly.[color=blue]
      >[color=green]
      >> asort($ballscho sen);[/color]
      >
      >Why use asort when you don't use the keys? See http://php.net/asort[/color]

      Seemed like a good idea at the time :)
      --
      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

      • Dominik Susmel

        #18
        Re: Not generating the same random number twice....

        Ewoud Dronkert wrote:[color=blue]
        > On Mon, 13 Jun 2005 23:58:32 +0200, Dominik Susmel wrote:[color=green]
        >> - seed the rand[/color]
        >
        > Not needed as of PHP4.2.0[/color]

        true...I don't know what he has
        [color=blue][color=green]
        >> - put nubers in array with array_rand[/color]
        >
        > That is not how array_rand() works, please see
        > http://php.net/array-rand[/color]

        duh...he said he has numbers? thus, populate the array and randomize it and
        trim it with _rand.. sorry, I thought it was obvious
        [color=blue][color=green]
        >> - use array_unique to initialize duplicate entries with 0[/color]
        >
        > Duplicate entries?! Bad algorithm. Also, that is not how
        > array_unique() works, please see http://php.net/array-unique[/color]

        that is how it works - check for yourself.. duplicates are not removed (if
        he has any - and shouldn't if he populates from db..)
        if you go through the array with it, keys are not removed - they are simply
        reinitialized ..look at your link that you've posted by yourself
        [color=blue]
        >[color=green]
        >> - do some magic to 0 numbers in contrast to existing stuff in array[/color]
        >
        > This is really helpful.[/color]

        yes, indeed it isn't - but that's only if he gets duplicate id's from db (if
        I remember correctly - he needs to randomize sequence from db?)...and if he
        gets duplicates from this..then magic is what is needed.. some form of
        deletion operations

        --
        --
        Dominik Susmel | art director
        www.vongestern.com | vonGestern art company . Zagreb, Croatia


        Comment

        • Dominik Susmel

          #19
          Re: Not generating the same random number twice....

          Ewoud Dronkert wrote:[color=blue]
          > On Mon, 13 Jun 2005 23:58:32 +0200, Dominik Susmel wrote:[color=green]
          >> - seed the rand[/color]
          >
          > Not needed as of PHP4.2.0
          >[color=green]
          >> - put nubers in array with array_rand[/color]
          >[/color]

          ok, I see he doesn't have image file names in db... so forget about key, val
          combo ;)

          --
          Dominik Susmel | art director
          www.vongestern.com | vonGestern art company . Zagreb, Croatia


          Comment

          Working...