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 8601
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 thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: james blair |
last post by:
Hi
I am generating a random number using
random.randint(1,10000000000)
Whats the possibility that the numbers generated will be same when
generated by 100 users at the same time?
Whats the best...
|
by: - |
last post by:
is it possible to generate a random number within an sql script rather
than the client specifying which random row to select?
|
by: VAL |
last post by:
How can I make an application form that generates a unique number starting
from 1001.?
|
by: Greg Strong |
last post by:
Hello All,
Is it possible to create multiple random numbers in a query where
there are numerous records?
I've created a custom function. When I use it in a query it creates
the same random...
|
by: vrkamalakar |
last post by:
Hi,
I need a piece of code to generate a random number between 0 and 5 (both exclusive) ie., number should be either 1,2,3 or 4. And also I need only two unique random numbers out of the four...
|
by: tshad |
last post by:
I have a page that I am getting a username and password as a random number
(2 letters, one number and 4 more letters)
I have 2 functions I call:
*************************************************...
|
by: pradeepjain |
last post by:
Hii guys,
I was using a a javascript for generating a random number.Some client screwed it up and it sent as text "random number " to database .So it became a problem.So i wanted a...
|
by: chanshaw |
last post by:
Alright I'm not sure if I worded the title correctly but I'm looking to generate a random number based on the numbers in an arraylist.
So if I have the values 1,2,12,6 I would like to generate a...
|
by: alishaikhji |
last post by:
I am working on a program which will need several different integer and float random numbers at different stages, for example:
- At one point, I need a random number (float) in the range 0.1 to 10.0...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |