Pseudo Random Sequence

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

    #1

    Pseudo Random Sequence

    Hi all,

    I am driving myself crazy trying to think of a solution to this - perhaps
    one of you guys can see a simple solution.

    The problem is displaying the contents of an array in what appears to be
    random order, but in fact is a set sequence depending on a given value (e.g.
    the day of the month).

    So on the 1st of the month the array is always displayed as:
    T,G,Q,C,I,N,E,A

    On the 2nd day you ALWAYS get
    G,I,A,N,E,T,Q,N

    The order itself is not important, but what is important is the apparent
    random sequence is really repeatable.

    Hope you can make some sense out of this.

    Many thanks,

    Nel.


  • Philip Ronan

    #2
    Re: Pseudo Random Sequence

    "Nel" wrote:
    [color=blue]
    > The problem is displaying the contents of an array in what appears to be
    > random order, but in fact is a set sequence depending on a given value (e.g.
    > the day of the month).[/color]

    (snip)
    [color=blue]
    > The order itself is not important, but what is important is the apparent
    > random sequence is really repeatable.[/color]

    Seed the random number generator based on the day of the month and use
    array_rand() to shuffle the contents

    $day = 1 * date('j');
    srand($day);
    $random_array = array_rand($arr ay);

    (I haven't tested this, but it ought to work)

    --
    phil [dot] ronan @ virgin [dot] net


    Comment

    • Ewoud Dronkert

      #3
      Re: Pseudo Random Sequence

      Philip Ronan wrote:[color=blue]
      > Seed the random number generator based on the day of the month and use
      > array_rand() to shuffle the contents[/color]

      Yep, only it's shuffle()...
      Gives the same results every time:

      $a = range('A', 'Z');
      for ( $i = 1; $i < 32; ++$i )
      {
      $b = $a;
      srand($i);
      shuffle($b);
      echo sprintf('%2d ', $i).implode(' ', $b)."\n";
      }

      --
      E. Dronkert

      Comment

      • Peter Fox

        #4
        Re: Pseudo Random Sequence

        Following on from Nel's message. . .[color=blue]
        >Hi all,
        >
        >I am driving myself crazy trying to think of a solution to this - perhaps
        >one of you guys can see a simple solution.[/color]
        The simple solution is to look at the documentation. What do you see
        there.... srand()

        --
        PETER FOX Not the same since the bra business went bust
        peterfox@eminen t.demon.co.uk.n ot.this.bit.no. html
        2 Tees Close, Witham, Essex.
        Gravity beer in Essex <http://www.eminent.dem on.co.uk>

        Comment

        • Nel

          #5
          Re: Pseudo Random Sequence


          "Ewoud Dronkert" <firstname@last name.net.invali d> wrote in message
          news:8k47n1hqmo dvvtiif2df2siok nd4n6i2h8@4ax.c om...[color=blue]
          > Philip Ronan wrote:[color=green]
          >> Seed the random number generator based on the day of the month and use
          >> array_rand() to shuffle the contents[/color]
          >
          > Yep, only it's shuffle()...
          > Gives the same results every time:
          >
          > $a = range('A', 'Z');
          > for ( $i = 1; $i < 32; ++$i )
          > {
          > $b = $a;
          > srand($i);
          > shuffle($b);
          > echo sprintf('%2d ', $i).implode(' ', $b)."\n";
          > }
          >
          > --
          > E. Dronkert[/color]

          Thanks to you both for your advice - unless you've come across such a
          feature before it's hard to know if such a thing is possible so easily.

          Thanks again!!!

          Nel.


          Comment

          Working...