Re: [PHP] Predictable-random array

2005-06-21 Thread Jochem Maas

Dotan Cohen wrote:

On 6/21/05, Jochem Maas <[EMAIL PROTECTED]> wrote:


Dotan Cohen wrote:










$items is a static file, that does not change over time. It just needs


that makes things easier :-)


to be output in a reproducable 'random' order for different purposes.
I looked over your suggested fuctions, but decided to try Marek's
simpler seed solution first. And it worked for what I need, so I will


indeed his is most to the point!


be going that route. But I very much appreciate your insight. Although
complicated, it presents a different way of solving the same problem.


the idea I proposed was more of a way to genericly (spelling?!?) hook in 
different
'random' ordering mechanisms - Marek's suggestion for ordering could be 
implemented
as a callback function in my example. e.g.

 $item) {
$keys[$k] = @$item{7};
}
asort($keys, SORT_LOCALE_STRING | SORT_STRING);
foreach ($keys as $k => $v) {
$items2[] = $items[$k];
}
return $items2;
}

function fixedShuffle1($items)
{
 static $fixedSeed = 384884;

 $keys = $items2 = array();
 // not sure whether seeding like this is a good idea
 // with regard to other, non-related code that relies
 // on the random generator... but anyway!
 foreach ($items as $k => $item) {
 $keys[] = $k;
 }
 srand( $fixedSeed );
 shuffle($keys);
 // srand( rand() ); // crap 'fix'?

 foreach ($keys as $k => $v) {
 $items2[] = $items[$k];
 }

 return $items2;
}

var_dump(permaRandom($items, "sortAlphaChar7Random"),
 permaRandom($items, "sortAlphaChar7Random"),
 permaRandom($items, "fixedShuffle1"),
 permaRandom($items, "fixedShuffle1"));

?>


That in itself is interesting. I intend to play with it a little when
I have some more time. Maybe I will find an advantage in your solution


there may be absolutely no advantage ;-)
I was mostly trying to cram as much learning material into the idea/example
as possible - kind of over engineering (in so far as you could call it 
engineering)
in order to show as many (hopefully) new ideas to who ever read it :-)

I'll be interested to know how you get on.

rgds,
Jochem


that I may need some day! Thank you.

Dotan
http://english-lyrics.com/
Song Lyrics


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Predictable-random array

2005-06-21 Thread Dotan Cohen
On 6/21/05, Jochem Maas <[EMAIL PROTECTED]> wrote:
> Dotan Cohen wrote:
> > I've got an array $items which has about 200 entires. I want to print
> 
> it might be helpful to know where the array comes from.
> or maybe its a static definition to start with (nothing wrong with that as 
> such :-)
> does $items change over time? in what way?
> 
> > them out in random order, but as usual, I have a catch! I need to add
> > a 'key' so that if need be, I can always return to the same 'random'
> > order, like so:
> >
> > $items=predicatable_random($items, "qwerty");
> 
> the problem is persistence, if you want a random order that doesn't change
> then you have to create the random order, and then store an index for it
> somewhere. it may be feasable to either serialize the result and read it from 
> disk
> when needed, or store a list of keys (an index of sorts) in file/db and
> use that to perform an ordering as required.
> 
> >
> > so for each place that I need the items rearranged, I will replace
> > qwerty with a different string, and get a different 'random' order. I
> > haven't played around with it much as I stumbled into a brick wall
> > fairly early and can't get much going. Any ideas?
> >
> > I tried to play with usort, uksort, ksort, and various combinations
> > with shuffle. The 'randomization' can be based on qualities of each
> > string, such as 'sort alphebetically by the 7th character', so I
> 
> thats not random. maybe the 'key' you need to pass is
> a callback function whose job it is to produce a replicable
> (or random for that matter!) ordering somehow - depending on the
> complexity of the calculation and size of the dataset you may
> need to cache (look at http://php.net/mt_srand if you need predictable
> [psuedo]randomness without cacheing (using 'fixed' seeds), and check
> the usernotes on http://php.net/shuffle also). e.g:
> 
> $items = array(
>  /* |--char 7 */
>  "cherries",
>  "bananas",
>  "orangejuice",
>  "red apples",
>  "smelly fish",
>  "more bananas",
>  "ship full of bananas",
>  "tarantula",
>  "poisoned apple",
>  /* |--char 7 */
> );
> 
> function permaRandom($items, $callback)
> {
>  /* caching left as an exercise to the reader ;-/ */
>  if (is_callable($callback)) {
>  $items2 = /*($yes = gotcachedpermarandom($callback))
>  ? getfromcache($callback)
>  : */call_user_func($callback, $items);
>  /* if ($yes) storeincache($callback, $items2); */
>  }
> 
>  return $items2;
> }
> 
> function sortAlphaChar7Random($items)
> {
>  $keys = $items2 = array();
>  // assumption made that $item is a string
>  // and at least 7 chars long, oh and spaces
>  // wont do much good for the sorting probably..
>  foreach ($items as $k => $item) {
>  $keys[$k] = @$item{7};
>  }
>  asort($keys, SORT_LOCALE_STRING | SORT_STRING);
>  foreach ($keys as $k => $v) {
>  $items2[] = $items[$k];
>  }
>  return $items2;
> }
> 
> var_dump(permaRandom($items, "sortAlphaChar7Random"),
>  permaRandom($items, "sortAlphaChar7Random"),
>  permaRandom($items, "sortAlphaChar7Random"));
> 
> > thought that I'd try that, but I got lost in explodes and such.
> >
> > Thanks all. I always look forward to my php lessons!
> >
> > Dotan
> > http://english-lyrics.com/el/index.php
> > Song Lyrics
> >
> 
> 

$items is a static file, that does not change over time. It just needs
to be output in a reproducable 'random' order for different purposes.
I looked over your suggested fuctions, but decided to try Marek's
simpler seed solution first. And it worked for what I need, so I will
be going that route. But I very much appreciate your insight. Although
complicated, it presents a different way of solving the same problem.
That in itself is interesting. I intend to play with it a little when
I have some more time. Maybe I will find an advantage in your solution
that I may need some day! Thank you.

Dotan
http://english-lyrics.com/
Song Lyrics

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Predictable-random array

2005-06-21 Thread Dotan Cohen
On 6/20/05, Marek Kilimajer <[EMAIL PROTECTED]> wrote:
> Dotan Cohen wrote:
> > I've got an array $items which has about 200 entires. I want to print
> > them out in random order, but as usual, I have a catch! I need to add
> > a 'key' so that if need be, I can always return to the same 'random'
> > order, like so:
> >
> > $items=predicatable_random($items, "qwerty");
> >
> > so for each place that I need the items rearranged, I will replace
> > qwerty with a different string, and get a different 'random' order. I
> > haven't played around with it much as I stumbled into a brick wall
> > fairly early and can't get much going. Any ideas?
> >
> > I tried to play with usort, uksort, ksort, and various combinations
> > with shuffle. The 'randomization' can be based on qualities of each
> > string, such as 'sort alphebetically by the 7th character', so I
> > thought that I'd try that, but I got lost in explodes and such.
> 
> Seed the generator with some integer using srand():
> 
> srand(384884);
> $a = range(0, 100);
> shuffle($a);
> print_r($a);
> 

Thanks! That's exactly the simple solution I was looking for. With the
current versions of php not needing a seed, I didn't even think to do
that!

I also recieved many other suggestions, and I will look into them. All
in the name of learning. Thank you.

Dotan
http://english-lyrics.com/el/index.php
English Song Lyrics

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Predictable-random array

2005-06-20 Thread Jochem Maas

Dotan Cohen wrote:

I've got an array $items which has about 200 entires. I want to print


it might be helpful to know where the array comes from.
or maybe its a static definition to start with (nothing wrong with that as such 
:-)
does $items change over time? in what way?


them out in random order, but as usual, I have a catch! I need to add
a 'key' so that if need be, I can always return to the same 'random'
order, like so:

$items=predicatable_random($items, "qwerty");


the problem is persistence, if you want a random order that doesn't change
then you have to create the random order, and then store an index for it
somewhere. it may be feasable to either serialize the result and read it from 
disk
when needed, or store a list of keys (an index of sorts) in file/db and
use that to perform an ordering as required.



so for each place that I need the items rearranged, I will replace
qwerty with a different string, and get a different 'random' order. I
haven't played around with it much as I stumbled into a brick wall
fairly early and can't get much going. Any ideas?

I tried to play with usort, uksort, ksort, and various combinations
with shuffle. The 'randomization' can be based on qualities of each
string, such as 'sort alphebetically by the 7th character', so I


thats not random. maybe the 'key' you need to pass is
a callback function whose job it is to produce a replicable
(or random for that matter!) ordering somehow - depending on the
complexity of the calculation and size of the dataset you may
need to cache (look at http://php.net/mt_srand if you need predictable
[psuedo]randomness without cacheing (using 'fixed' seeds), and check
the usernotes on http://php.net/shuffle also). e.g:

$items = array(
/* |--char 7 */
"cherries",
"bananas",
"orangejuice",
"red apples",
"smelly fish",
"more bananas",
"ship full of bananas",
"tarantula",
"poisoned apple",
/* |--char 7 */
);

function permaRandom($items, $callback)
{
/* caching left as an exercise to the reader ;-/ */
if (is_callable($callback)) {
$items2 = /*($yes = gotcachedpermarandom($callback))
? getfromcache($callback)
: */call_user_func($callback, $items);
/* if ($yes) storeincache($callback, $items2); */
}

return $items2;
}

function sortAlphaChar7Random($items)
{
$keys = $items2 = array();
// assumption made that $item is a string
// and at least 7 chars long, oh and spaces
// wont do much good for the sorting probably..
foreach ($items as $k => $item) {
$keys[$k] = @$item{7};
}
asort($keys, SORT_LOCALE_STRING | SORT_STRING);
foreach ($keys as $k => $v) {
$items2[] = $items[$k];
}
return $items2;
}

var_dump(permaRandom($items, "sortAlphaChar7Random"),
 permaRandom($items, "sortAlphaChar7Random"),
 permaRandom($items, "sortAlphaChar7Random"));


thought that I'd try that, but I got lost in explodes and such.

Thanks all. I always look forward to my php lessons!

Dotan
http://english-lyrics.com/el/index.php
Song Lyrics



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Predictable-random array

2005-06-20 Thread Marek Kilimajer

Dotan Cohen wrote:

I've got an array $items which has about 200 entires. I want to print
them out in random order, but as usual, I have a catch! I need to add
a 'key' so that if need be, I can always return to the same 'random'
order, like so:

$items=predicatable_random($items, "qwerty");

so for each place that I need the items rearranged, I will replace
qwerty with a different string, and get a different 'random' order. I
haven't played around with it much as I stumbled into a brick wall
fairly early and can't get much going. Any ideas?

I tried to play with usort, uksort, ksort, and various combinations
with shuffle. The 'randomization' can be based on qualities of each
string, such as 'sort alphebetically by the 7th character', so I
thought that I'd try that, but I got lost in explodes and such.


Seed the generator with some integer using srand():

srand(384884);
$a = range(0, 100);
shuffle($a);
print_r($a);

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Predictable-random array

2005-06-20 Thread Dotan Cohen
I've got an array $items which has about 200 entires. I want to print
them out in random order, but as usual, I have a catch! I need to add
a 'key' so that if need be, I can always return to the same 'random'
order, like so:

$items=predicatable_random($items, "qwerty");

so for each place that I need the items rearranged, I will replace
qwerty with a different string, and get a different 'random' order. I
haven't played around with it much as I stumbled into a brick wall
fairly early and can't get much going. Any ideas?

I tried to play with usort, uksort, ksort, and various combinations
with shuffle. The 'randomization' can be based on qualities of each
string, such as 'sort alphebetically by the 7th character', so I
thought that I'd try that, but I got lost in explodes and such.

Thanks all. I always look forward to my php lessons!

Dotan
http://english-lyrics.com/el/index.php
Song Lyrics

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php