Jump to content

[SOLVED] Restore a random sequence


idlelimey

Recommended Posts

Hi, not sure if this works, but I've written a very simple script to generate a pattern of random tiles using a specific quantity of each colour (it's for a patchwork quilt).

 

It works fine, but I'd like to be able to restore a sequence with a number.  So, although it's random, I could type in '123456' and that would restore it.  Is there a way of using a number to control the randomisation?

 

I really don't know what to search for to find help on this, and I appreciate that my explaination is woeful at best.

 

Any help welcome :)

 

Thank you.

Link to comment
https://forums.phpfreaks.com/topic/161484-solved-restore-a-random-sequence/
Share on other sites

<?php
    mt_srand(123456);
    echo mt_rand(1, 100), "\n";
    echo mt_rand(1, 100), "\n";
    echo mt_rand(1, 100), "\n";

 

Those three will output the same number To get a different number you'd need a different mt_srand for each and store them somewhere.

Thanks again, with your help I ended up with this:

 

$tiles = $array;
if(isset($_GET['seed']))
  {$seed = $_GET['seed'];}
else
  {rand(1,999999);}
echo'<p>'.$seed.'</p>';
srand($seed);
shuffle($tiles);
while(list(, $tile) = each($tiles)){ ... }

 

Seems to work fine.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.