Hi Yall
I have a page where I want 5 images to appear, randomly selected from a pool
of 50 images called 1.jpg to 50.jpg
The following code works nicely, except for one (probably obvious)
problem...
$count=1;
do {
$random = rand(1,50);
echo "<img src='images/$random.jpg'>";
$count++;
}
while ($count<5);
The problem is that there is nothing to stop images appearing more than
once. I do not want this to happen...
Any ideas on a possible solution?
TIA
Mark 18 8456
Mark said the following on 13/06/2005 19:59: Hi Yall
I have a page where I want 5 images to appear, randomly selected from a pool of 50 images called 1.jpg to 50.jpg
The following code works nicely, except for one (probably obvious) problem...
$count=1; do { $random = rand(1,50); echo "<img src='images/$random.jpg'>"; $count++; } while ($count<5);
The problem is that there is nothing to stop images appearing more than once. I do not want this to happen...
Any ideas on a possible solution?
Set up an array of 50 Booleans, each initially FALSE. Every time a
random number is chosen, check to see if the corresponding entry in the
array is FALSE. If it is, set it to TRUE, and use that number. If it is
already TRUE, choose another random number.
--
Oli
1. Use range() to create an array with number 1 - 50.
2. Use shuttle() to shuffle the numbers.
3. Save the array in session variable.
4. Use the proper indices, depending on which page the user's is on, to
obtain 5 random numbers from the array.
On Mon, 13 Jun 2005 19:59:25 +0100, "Mark" <ma**@ilovespam.com> wrote: I have a page where I want 5 images to appear, randomly selected from a pool of 50 images called 1.jpg to 50.jpg
The following code works nicely, except for one (probably obvious) problem...
The problem is that there is nothing to stop images appearing more than once. I do not want this to happen...
Any ideas on a possible solution? http://uk2.php.net/range http://uk.php.net/array_rand
<?php print_r(array_rand(range(1,50), 5)); ?>
It uses the keys rather than values so you'll probably want to add one when
making the filename.
--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Chung Leong <ch***********@hotmail.com> wrote: 1. Use range() to create an array with number 1 - 50. 2. Use shuttle() to shuffle the numbers.
That's shuffle not shuttle. ;)
3. Save the array in session variable. 4. Use the proper indices, depending on which page the user's is on, to obtain 5 random numbers from the array.
Why not simply use array_pop() to get through the 50 images? Then when
the array is empty, start at step one again...
Taking 5 random entries from a shuffled (random) array doesn't quite
make sense to me, or am I missing something?
--
Justin Koivisto - ju****@koivi.com http://koivi.com
On Mon, 13 Jun 2005 19:06:29 GMT, Oli Filth wrote: The problem is that there is nothing to stop images appearing more than once. I do not want this to happen...
Set up an array of 50 Booleans, each initially FALSE. Every time a random number is chosen, check to see if the corresponding entry in the array is FALSE. If it is, set it to TRUE, and use that number. If it is already TRUE, choose another random number.
Bad idea, generally. In this particular case, with these small numbers
and 5 rather smaller than 50, it will probably not be problematic, but
what if you're trying to generate the whole range in random order and
that darn last number just won't pop up..? Easy solution:
$a = range( 1, 50 );
$b = shuffle( $a );
for ( $i = 0; $i < 5; ++$i )
echo $b[$i]."\n";
Or written out:
$n = 50; // range 1..n
$k = 5; // 1 <= k <= n
$a = range( 1, $n );
for ( $i = 0; $i < $k; ++$i )
{
$j = mt_rand( $i, $n - 1 );
echo $a[$j]."\n";
$a[$j] = $a[$i];
}
--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
I'm just guessing that the script involves paging through a list of
fifty images, five at a time. If that's case you'd need to have the
same array as you go from page to page. Otherwise then, yes, as you
said, you can just pop 5 numbers off the array.
Another way to maintain the same randomized array across pages is to
seed the random number engine with a fixed number, like today's date. A
useful trick when you don't want to use session.
Thanks for your replies guys (n gals?)
I will try them all and let you know who was "THE CHOSEN ONE"
Thanks again!
Mark
"Mark" <ma**@ilovespam.com> wrote in message
news:1118689179.0bfe721d1be71944ad69de19a67d3613@t eranews... Hi Yall
I have a page where I want 5 images to appear, randomly selected from a pool of 50 images called 1.jpg to 50.jpg
The following code works nicely, except for one (probably obvious) problem...
$count=1; do { $random = rand(1,50); echo "<img src='images/$random.jpg'>"; $count++; } while ($count<5);
The problem is that there is nothing to stop images appearing more than once. I do not want this to happen...
Any ideas on a possible solution?
TIA
Mark
- seed the rand
- put nubers in array with array_rand
- use array_unique to initialize duplicate entries with 0
- do some magic to 0 numbers in contrast to existing stuff in array
--
Dominik Susmel | art director www.vongestern.com | vonGestern art company . Zagreb, Croatia
I noticed that Message-ID:
<1118689179.0bfe721d1be71944ad69de19a67d3613@teran ews> from Mark
contained the following: The problem is that there is nothing to stop images appearing more than once. I do not want this to happen...
Any ideas on a possible solution?
The following picks lottery balls (6 unique balls out of 49) Trivial to
adapt it to what you want.
<html>
<body style="color:red"><?php
for($i=1;$i<50;$i++){
$balls[]=$i;
}
for($i=0;$i<6;$i++){
$key=array_rand($balls);
$ballschosen[]=$balls[$key];
unset($balls[$key]);
}
asort($ballschosen);
foreach($ballschosen as $choice){
print"$choice ";
}
?>
</body>
</html>
--
Geoff Berrow 0110001001101100010000000110
001101101011011001000110111101100111001011
100110001101101111001011100111010101101011
<ch***********@hotmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com... I'm just guessing that the script involves paging through a list of fifty images, five at a time. If that's case you'd need to have the same array as you go from page to page. Otherwise then, yes, as you said, you can just pop 5 numbers off the array.
Another way to maintain the same randomized array across pages is to seed the random number engine with a fixed number, like today's date. A useful trick when you don't want to use session.
Isn't this doing double work? The shuffle randomizes the array. Then, all
you need to do is take the first five elements, since they are already in
random order. -- or am I missing something?
Shelly
On Mon, 13 Jun 2005 23:58:32 +0200, Dominik Susmel wrote: - seed the rand
Not needed as of PHP4.2.0
- put nubers in array with array_rand
That is not how array_rand() works, please see http://php.net/array-rand
- use array_unique to initialize duplicate entries with 0
Duplicate entries?! Bad algorithm. Also, that is not how array_unique()
works, please see http://php.net/array-unique
- do some magic to 0 numbers in contrast to existing stuff in array
This is really helpful.
--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
On Mon, 13 Jun 2005 23:54:08 +0100, Geoff Berrow wrote: for($i=1;$i<50;$i++){ $balls[]=$i; }
See http://php.net/range
for($i=0;$i<6;$i++){ $key=array_rand($balls); $ballschosen[]=$balls[$key]; unset($balls[$key]); }
See http://php.net/shuffle
asort($ballschosen);
Why use asort when you don't use the keys? See http://php.net/asort
foreach($ballschosen as $choice){ print"$choice "; }
--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
[snip]
All these much more sophisticated solutions make me wonder where I went
wrong in the early days.
while $Result etc
{
$Query="select image_filename from Tablename order by rand() limit 5";
images[]=$Row[image_filename];
}
Then position $images[0] $images[1] $images[2] where you want them.
Is there a prize for the neatest one? </pant-pant>
On Tue, 14 Jun 2005 08:35:44 GMT, nemo wrote: All these much more sophisticated solutions make me wonder where I went wrong in the early days. $Query="select image_filename from Tablename order by rand() limit 5";
1. His image filenames are not in a db :)
2. Order by rand potentially causes enormous overhead. See for example http://jan.kneschke.de/projects/mysql/order-by-rand/ (performance data
at bottom of page).
--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
I noticed that Message-ID: <av********************************@4ax.com>
from Ewoud Dronkert contained the following: On Mon, 13 Jun 2005 23:54:08 +0100, Geoff Berrow wrote: for($i=1;$i<50;$i++){ $balls[]=$i; } See http://php.net/range
Code is from a course I run and it's a good idea to get the students
used to using loops for($i=0;$i<6;$i++){ $key=array_rand($balls); $ballschosen[]=$balls[$key]; unset($balls[$key]); } See http://php.net/shuffle
For some reason I've never been able to get shuffle to work properly. asort($ballschosen);
Why use asort when you don't use the keys? See http://php.net/asort
Seemed like a good idea at the time :)
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Ewoud Dronkert wrote: On Mon, 13 Jun 2005 23:58:32 +0200, Dominik Susmel wrote: - seed the rand Not needed as of PHP4.2.0
true...I don't know what he has - put nubers in array with array_rand
That is not how array_rand() works, please see http://php.net/array-rand
duh...he said he has numbers? thus, populate the array and randomize it and
trim it with _rand.. sorry, I thought it was obvious - use array_unique to initialize duplicate entries with 0
Duplicate entries?! Bad algorithm. Also, that is not how array_unique() works, please see http://php.net/array-unique
that is how it works - check for yourself.. duplicates are not removed (if
he has any - and shouldn't if he populates from db..)
if you go through the array with it, keys are not removed - they are simply
reinitialized ..look at your link that you've posted by yourself - do some magic to 0 numbers in contrast to existing stuff in array
This is really helpful.
yes, indeed it isn't - but that's only if he gets duplicate id's from db (if
I remember correctly - he needs to randomize sequence from db?)...and if he
gets duplicates from this..then magic is what is needed.. some form of
deletion operations
--
--
Dominik Susmel | art director www.vongestern.com | vonGestern art company . Zagreb, Croatia
Ewoud Dronkert wrote: On Mon, 13 Jun 2005 23:58:32 +0200, Dominik Susmel wrote: - seed the rand
Not needed as of PHP4.2.0
- put nubers in array with array_rand
ok, I see he doesn't have image file names in db... so forget about key, val
combo ;)
--
Dominik Susmel | art director www.vongestern.com | vonGestern art company . Zagreb, Croatia This discussion thread is closed Replies have been disabled for this discussion. Similar topics
4 posts
views
Thread by james blair |
last post: by
|
2 posts
views
Thread by - |
last post: by
|
6 posts
views
Thread by VAL |
last post: by
|
4 posts
views
Thread by Greg Strong |
last post: by
| |
3 posts
views
Thread by tshad |
last post: by
| | | | | | | | | | | | | |