Connecting Tech Pros Worldwide Help | Site Map

Pseudo Random Sequence

 
LinkBack Thread Tools Search this Thread
  #1  
Old November 10th, 2005, 04:45 PM
Nel
Guest
 
Posts: n/a
Default 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.



  #2  
Old November 10th, 2005, 05:25 PM
Philip Ronan
Guest
 
Posts: n/a
Default 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($array);

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

--
phil [dot] ronan @ virgin [dot] net
http://vzone.virgin.net/phil.ronan/

  #3  
Old November 10th, 2005, 05:45 PM
Ewoud Dronkert
Guest
 
Posts: n/a
Default 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
  #4  
Old November 10th, 2005, 06:25 PM
Peter Fox
Guest
 
Posts: n/a
Default 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@eminent.demon.co.uk.not.this.bit.no.html
2 Tees Close, Witham, Essex.
Gravity beer in Essex <http://www.eminent.demon.co.uk>
  #5  
Old November 10th, 2005, 06:55 PM
Nel
Guest
 
Posts: n/a
Default Re: Pseudo Random Sequence


"Ewoud Dronkert" <firstname@lastname.net.invalid> wrote in message
news:8k47n1hqmodvvtiif2df2sioknd4n6i2h8@4ax.com...[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.


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.