Re: [PHP] Random pictures - not twice

2006-12-04 Thread tedd

At 11:20 PM +0100 12/3/06, Gustav Wiberg wrote:

Hi there!

I have created a script the generates random pictures...


-snip-

With this above script the same picture can be shown twice. Is there 
any smart way of avoiding this without having to rely on 
cookies/sessionids?


Random does mean it can happen more than once. I think what you want 
is more along a shuffle. Look into the array function shuffle.


Cheers,

tedd
--
---
http://sperling.com  http://ancientstones.com  http://earthstones.com

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



Re: [PHP] Random pictures - not twice

2006-12-04 Thread Gustav Wiberg


- Original Message - 
From: tedd [EMAIL PROTECTED]
To: Gustav Wiberg [EMAIL PROTECTED]; PHP General 
php-general@lists.php.net

Sent: Monday, December 04, 2006 4:15 PM
Subject: Re: [PHP] Random pictures - not twice



At 11:20 PM +0100 12/3/06, Gustav Wiberg wrote:

Hi there!

I have created a script the generates random pictures...


-snip-

With this above script the same picture can be shown twice. Is there any 
smart way of avoiding this without having to rely on cookies/sessionids?


Random does mean it can happen more than once. I think what you want is 
more along a shuffle. Look into the array function shuffle.


Cheers,

tedd
--
---
http://sperling.com  http://ancientstones.com  http://earthstones.com

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


Hi there!

Ok, thanx! I will check the function out :-)

Best regards
Gsutav Wiberg
http://www.stammis.com/ - Stammis Internet - pedigrees on the net

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



[PHP] Random pictures - not twice

2006-12-03 Thread Gustav Wiberg

Hi there!

I have created a script the generates random pictures...

?php
//Random pictures
//
$pictures[0] = bil1.gif;
$pictures[1] = bil2.gif;
$pictures[2] = bil3.gif;
$pictures[3] = bil4.gif;


for ($i=0;$i3;$i++) {
 $r = rand(0,3);
?
pnbsp;nbsp;img src=?php echo $pictures[$r];?/p
?php

}
?


With this above script the same picture can be shown twice. Is there any 
smart way of avoiding this without having to rely on cookies/sessionids?


Best regards
/Gustav Wiberg
Stammis Internet - http://www.stammis.com/ - pedigrees on the net

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



Re: [PHP] Random pictures - not twice

2006-12-03 Thread tg-php
Without using cookies or session information, you're going to go through your 
picture list faster depending on how many users are accessing the random pic 
page.  If you don't make it user-specific, then it doesn't really matter if you 
go through the images sequentially.. it may appear random on the user end 
because other users are going to be grabbing sequential pics as well.

If you want to stir the waters a bit, you could run them sequentially but 
randomize the sequence.  This is what some music playlist programs do when you 
randomize a playlist.  Instead of pulling a random song from the list and 
risking playing the same song back to back, it just shuffles the playlist and 
plays it sequentially.  When you get to the end of your randomized list, you 
can re-shuffle it and start over.  This gives some semblance of randomness.

If you want to make sure each image gets its fair amount of time in the 
spotlight,  you could keep track of how many times each image has been 
displayed.  Having a database table with a list of all the image names and 
their 'served' count would let you weight your list a bit to give preference to 
the images that havn't been displayed that often.


And if you want to give each user the total package, you can keep track of how 
many times each image was served to a specific user and weight your shuffling 
that way.


I'm sure there are other ways, but there are just a few that may be viable for 
you, depending on the level of randomness per user you need.

Let us know what solution you come up with.  I'm sure others have had similar 
questions and just havn't asked.

-TG

= = = Original message = = =

Hi there!

I have created a script the generates random pictures...

?php
//Random pictures
//
$pictures[0] = bil1.gif;
$pictures[1] = bil2.gif;
$pictures[2] = bil3.gif;
$pictures[3] = bil4.gif;


for ($i=0;$i3;$i++) 
  $r = rand(0,3);
?
pnbsp;nbsp;img src=?php echo $pictures[$r];?/p
?php


?


With this above script the same picture can be shown twice. Is there any 
smart way of avoiding this without having to rely on cookies/sessionids?

Best regards
/Gustav Wiberg
Stammis Internet - http://www.stammis.com/ - pedigrees on the net



___
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.

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



Re: [PHP] Random pictures - not twice

2006-12-03 Thread Gustav Wiberg
- Original Message - 
From: [EMAIL PROTECTED]

To: php-general@lists.php.net
Cc: [EMAIL PROTECTED]
Sent: Sunday, December 03, 2006 11:52 PM
Subject: Re: [PHP] Random pictures - not twice


Without using cookies or session information, you're going to go through 
your picture list faster depending on how many users are accessing the 
random pic page.  If you don't make it user-specific, then it doesn't 
really matter if you go through the images sequentially.. it may appear 
random on the user end because other users are going to be grabbing 
sequential pics as well.


If you want to stir the waters a bit, you could run them sequentially but 
randomize the sequence.  This is what some music playlist programs do when 
you randomize a playlist.  Instead of pulling a random song from the list 
and risking playing the same song back to back, it just shuffles the 
playlist and plays it sequentially.  When you get to the end of your 
randomized list, you can re-shuffle it and start over.  This gives some 
semblance of randomness.


If you want to make sure each image gets its fair amount of time in the 
spotlight,  you could keep track of how many times each image has been 
displayed.  Having a database table with a list of all the image names and 
their 'served' count would let you weight your list a bit to give 
preference to the images that havn't been displayed that often.



And if you want to give each user the total package, you can keep track of 
how many times each image was served to a specific user and weight your 
shuffling that way.



I'm sure there are other ways, but there are just a few that may be viable 
for you, depending on the level of randomness per user you need.


Let us know what solution you come up with.  I'm sure others have had 
similar questions and just havn't asked.


-TG

= = = Original message = = =

Hi there!

I have created a script the generates random pictures...

?php
//Random pictures
//
$pictures[0] = bil1.gif;
$pictures[1] = bil2.gif;
$pictures[2] = bil3.gif;
$pictures[3] = bil4.gif;


for ($i=0;$i3;$i++)
 $r = rand(0,3);
?
pnbsp;nbsp;img src=?php echo $pictures[$r];?/p
?php


?


With this above script the same picture can be shown twice. Is there any
smart way of avoiding this without having to rely on cookies/sessionids?

Best regards
/Gustav Wiberg
Stammis Internet - http://www.stammis.com/ - pedigrees on the net



___
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.

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



Hi again!

I've solved it with array_rand. It wasn't that hard at all, just had to know 
which function to use! :-)


code:
?php
//Random pictures
//
$pictures[0] = bil1.gif;
$pictures[1] = bil2.gif;
$pictures[2] = bil3.gif;
$pictures[3] = bil4.gif;

$randomNr = array_rand($pictures, count($pictures));
$occurences = 3;

for ($i=0;$i$occurences;$i++) {
?
img src=?php echo $pictures[$randomNr[$i]];?
?php
}

?
Best regards
/Gustav Wiberg
Stammis Internet - http://www.stammis.com/ - pedigrees on the net

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