Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How can I select 2 random images from 20 in JSP?

Posted on 2003-11-21
12
Medium Priority
?
827 Views
Last Modified: 2010-04-01
hi ...

i need to be able to select 2 images at random from approx 20 images and display them on a page.

at this point i am assuming that the simplest route will be to have the image filenames in an array and then pick 2 of them to display. any better solutions will be appreciated but i have no database access.

i will need to know how to set up the array and then select two at random and write the selected values to the HTML.

if someone could please help with some code that will sort this out for me i would be a happy bunny.

thanks!
0
Comment
Question by:equentin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 4
12 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 9797147
say all your images are in a "images" folder (under the jsp folder)

Also, assume they are all named

1.jpg -> 20.jpg

Then you can do:

<img src="images/<%= "" + (int)( Math.random * 20 ) %>">
<img src="images/<%= "" + (int)( Math.random * 20 ) %>">

But I guess you want more intelligent code than that ;-)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9797152
Oops

<img src="images/<%= "" + (int)( Math.random * 20 ) %>.jpg">
<img src="images/<%= "" + (int)( Math.random * 20 ) %>.jpg">
0
 

Author Comment

by:equentin
ID: 9797221
tim, i do need to have the filenames (in an array, or as variables) and not just a number and i also need to ensure that the same random number is not duplicated.

any thoughts?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 35

Expert Comment

by:TimYates
ID: 9797499
Stopping the same random number is easy:

int one = (int)( Math.random() * array.length ) ;
int two = one ;
while( two == one )
    two = (int)( Math.random() * array.length ) ;

As for the array of images... hmmm

are the images inside a folder in the same webapp as this jsp?

Is it just the images in that folder?
0
 

Author Comment

by:equentin
ID: 9797564
thanks ..

the images are in an images folder off the site root. all links on the site are site root relative. so all images are src="/assets/image.jpg"

there must be a way to have a list of image filenames in an array and pull out items [random x] and [random y]?

0
 
LVL 35

Expert Comment

by:TimYates
ID: 9797711
you can do:


<%!
  class MyFilter implements java.io.FilenameFilter
  {
      public boolean accept( java.io.File dir, String name )
      {
          name = name.toUpperCase() ;
          return name.endsWith(".JPG") || name.endsWith(".PNG") || name.endsWith(".GIF") ;
      }
  }
%>

<%
    String pathToTomcat = "/opt/tomcat/webapps/ROOT/" ;
    String pathToAssets = "assets/" ;

    String[] array = new java.io.File( pathToTomcat + pathToAssets ).list( new MyFilter() ) ;

    for( int i = 0 ; i < array.length ; i++ )
    {
        array[ i ] = array[ i ].substring( pathToTomcat.length() ) ;
    }

    int one = (int)( Math.random() * array.length ) ;
    int two = one ;
    while( two == one )
        two = (int)( Math.random() * array.length ) ;
%>

<img src="images/<%= array[ one ] %>">
<img src="images/<%= array[ two ] %>">
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9797714
or something similar ;-)

hee hee
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9797734
Sorry...that's wrong...

This should work:

----------------------

<%!
  class MyFilter implements java.io.FilenameFilter
  {
      public boolean accept( java.io.File dir, String name )
      {
          name = name.toUpperCase() ;
          return name.endsWith(".JPG") || name.endsWith(".PNG") || name.endsWith(".GIF") ;
      }
  }
%>

<%
    String pathToAssets = "/opt/tomcat/webapps/ROOT/assets/" ;

    String[] array = new java.io.File( pathToAssets ).list( new MyFilter() ) ;

    int one = (int)( Math.random() * array.length ) ;
    int two = one ;
    while( two == one )
        two = (int)( Math.random() * array.length ) ;
%>

<img src="assets/<%= array[ one ] %>">
<img src="assets/<%= array[ two ] %>">
0
 

Author Comment

by:equentin
ID: 9797821
nearly there!

actually, perhaps i should have mentioned that there are a million other images in the assets folder and that i will need to manually populate the array ...

;P
0
 
LVL 35

Accepted Solution

by:
TimYates earned 1000 total points
ID: 9797849
hahahaha....perhaps ;-)

Does that work then?  Does it pick two random assets?

Cool :-)

just make a static array then:

change the code to:

---------------------

<%
    String[] array = new String[] { "image1.jpg", "image2.gif", "blah.jpg", etc... } ;

    int one = (int)( Math.random() * array.length ) ;
    int two = one ;
    while( two == one )
        two = (int)( Math.random() * array.length ) ;
%>

<img src="assets/<%= array[ one ] %>">
<img src="assets/<%= array[ two ] %>">
0
 

Author Comment

by:equentin
ID: 9797914
you are a star ... that's perfect

thanks :)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9797934
hee hee...

Glad I could help :-)

Tim
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I’ll show how research, determination, and use of modern technology helped me solve a DNA mystery.
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question