Win a copy of Get Programming with Java (MEAP only) this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

How i can shuffle images  RSS feed

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys im with my first java/android app

I want shuffle 5 images i already check on google "how to shuffle images java" and "how to shuffle a deck on java" and i read stackoverflow but they explain the logic for shuffle the values of cards  and i only want shuffle the images

If anyone can share a link or explainme what i need learn for add this effect will be great

thanks in advance for read this
 
Master Rancher
Posts: 933
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Welcome to the ranch .
You're talking about a card deck right? How do you keep track of your collection of cards?
Lets say you have a list of cards, eather created the list yourself or retrieved it from a db, then you would simply generated 5 random numbers smaller or equal to 52 and use those as index for you list.

Code Example:

The same logic you could aply to retrieve a single card from the db, instead of generating a random listIndex, you would generate a random id (would only work if you autoincrement your id ofcourse, and don't use some custom id).
keep in mind that then you would do
cause like a list random starts counting from zero, but the id of a tabel start at 1, so by doing +1 you add one to the random generated number. If rand generated 0 it becomes 1, 1 becomes 2 and so on
 
German Hernandez
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi daniel thank you a lot for the answer but i need something more simple no values only the images
 
German Hernandez
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry but i cant edit the previous post i will try with this code removing the extra data, thanks for the help !
 
Daniel Demesmaecker
Master Rancher
Posts: 933
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

German Hernandez wrote:hi daniel thank you a lot for the answer but i need something more simple no values only the images


Lol, sorry to disapoint you, but mutch more simpler then that it's not gonna get
 
Daniel Demesmaecker
Master Rancher
Posts: 933
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you could try
But that wont give you five cards, that will shuffle the whole deck
 
Ranch Hand
Posts: 42
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

German Hernandez wrote:Hi guys im with my first java/android app

I want shuffle 5 images i already check on google "how to shuffle images java" and "how to shuffle a deck on java" and i read stackoverflow but they explain the logic for shuffle the values of cards  and i only want shuffle the images

If anyone can share a link or explainme what i need learn for add this effect will be great

thanks in advance for read this



Hi German Hernandez

1. random to get a number between 0 - 4
2. array of images
3. set the Imageview id

4.  set the imageview equal to the array[random number ]

You will get an random image



 
Daniel Demesmaecker
Master Rancher
Posts: 933
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And how is that different from my first post?
 
Marshal
Posts: 62259
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best way to shuffle a List is Collections#shuffle. Only create your own algorithm if you are taking an algorithms module and have been told to do so. There are discussions about shuffling here and Bloch and Gafter Java Puzzlers, page 233‑237 and other discussions here, e.g. here.
 
Campbell Ritchie
Marshal
Posts: 62259
193
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's remember the original intent of this thread, which is to get five random images. It doesn't actually matter what is in the List; Collections#shuffle(List) can take any kind of List. So after you have learnt to shuffle a List<Card> you can simply substitute a List<Image> or similar. Maybe you would do well to start with a List<Integer> and shuffle that, because you can simply sort that with myNumberList.sort() and then you can see the difference when you shuffle it.
Please explain what you are doing. Are you shuffling a 5‑element List or a 52‑element List and choosing five elements? Do all five have to be distinct (i.e. no duplicates)? In which case you are not doing random selection but random selection from a declining population, which is different. Maybe the quickest way to implement random selection from a reducing population is to do the following:-
  • 1: (Optional) Copy the source List, so as not to alter the original.
  • 2: Use a source of “random” numbers between 0 (inclusive) and size of the List exclusive.
  • 3: Remove the element at that index. This is the element you want.
  • 4: Repeat until the List is empty, or enough elements have been selected. Remember the maximum possible index will gradually decline.
  •  
    jason edwardes
    Ranch Hand
    Posts: 42
    1
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Daniel Demesmaecker wrote:And how is that different from my first post?



    none your right Daniel
    My phone email notification has been showing me questions on code-ranch, so clicked the link to answer this question, unfortunately the link did not include any answers that you have given
     
    Daniel Demesmaecker
    Master Rancher
    Posts: 933
    17
    Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    jason edwardes wrote:

    Daniel Demesmaecker wrote:And how is that different from my first post?



    none your right Daniel
    My phone email notification has been showing me questions on code-ranch, so clicked the link to answer this question, unfortunately the link did not include any answers that you have given



    Just for being so cool about it, have a cow. I removed your downvote to, mistakes happen and I didn't mean to be an ***
     
    Daniel Demesmaecker
    Master Rancher
    Posts: 933
    17
    Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:... In which case you are not doing random selection but random selection from a declining population, which is different. Maybe the quickest way to implement random selection from a reducing population is to do the following:-

  • 1: (Optional) Copy the source List, so as not to alter the original.
  • 2: Use a source of “random” numbers between 0 (inclusive) and size of the List exclusive.
  • 3: Remove the element at that index. This is the element you want.
  • 4: Repeat until the List is empty, or enough elements have been selected. Remember the maximum possible index will gradually decline.


  • Wouldn't that also mean you would have to create a new deck of cards everytime you wan't to play a new game without restarting the program? Dosn't seem so efficient to me
     
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    ou can't randomly arrange photos in a gallery. But it is possible to grab a random image from a gallery. See http://wiki.smugmug.com/display/SmugMug/Random+Photos+Code. I use that code to grab a random image for my header, and to grab the random images for my Gallery page and categories pages.
     
    Marshal
    Posts: 5635
    147
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    nate liv: Your URL points to a site that is not secure, and often members are reluctant to click a link that isn't well known, like GitHub.  Either post your code here, or if it's too big, upload it to GitHub and post the URL.
     
    Campbell Ritchie
    Marshal
    Posts: 62259
    193
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Daniel Demesmaecker wrote:. . . Wouldn't that also mean you would have to create a new deck of cards everytime you wan't to play a new game without restarting the program? Dosn't seem so efficient to me

    Not quite. It would be possible to remove the cards from the original List, but I have done enough reversible programming to have a prejudice against losing information like that. It is also possible to replace the cards in the List as they are played, Both those actions together will return the List to 52 elements, but it will still be necessary to shuffle it again. Copying the original List allows you to retain information about its contents and ordering. It doesn't take much processor time to copy a small List like that. If you make the card objects immutable, they can easily be reused, so you only need to create a new List and a new backing array.

    As they say in London, yer pays yer money and yer takes yer choice. Each of your choices will have a slightly different outcome.
     
    German Hernandez
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    thanks all for take time for help me but i need something more simple i did this ugly graphic maybe is more clear

    http://es.tinypic.com/view.php?pic=1h73va&s=9#.XBsKEd9fg5k
     
    Knute Snortum
    Marshal
    Posts: 5635
    147
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I can't see any graphic at that URL.  What about adding the image as an attachment?
     
    German Hernandez
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    added as attachment
    template.jpg
    [Thumbnail for template.jpg]
     
    Knute Snortum
    Marshal
    Posts: 5635
    147
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Have you done what Campbell Ritchie suggested here?
     
    German Hernandez
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    yes, problem solved, thanks
     
    Liar, liar, pants on fire! refreshing plug:
    Programmatically Create PDF Using Free Spire.PDF with Java
    https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!