A random shuffle

If I want to create a continuous loop to keep on shuffling until it gets the same sequence of numbers as the original String. How do I do that?
public class MyClass
     private Object[] myArray = null;
     public static void main(String[] args)
          MyClass shuffler =     new MyClass(new String[] {"1", "2", "3", "4","5","6","7","8"});
          System.out.println(shuffler);
          shuffler.shuffle();
          System.out.println(shuffler);
          shuffler.shuffle();
          System.out.println(shuffler);
     public MyClass(Object[] anArray)
               myArray = anArray;
     private void shuffle()
               java.util.Random rnd = new java.util.Random();
               for (int n = 0; n < myArray.length; n++)
                    int j = rnd.nextInt(myArray.length);
                    Object o = myArray[j];
                    myArray[j] = myArray[n];
                    myArray[n] = o;
          public String toString()
               String s = myArray[0].toString();
               for (int j = 1; j < myArray.length; j++)
                    s += ", " + myArray[j];
          return s;
}

Here's a java way of handling the shuffling back to the original using cloning, the .equals method inherited from Object (and then overridden), a single instance of Random regardless of the number of decks and a little polymorphism...
Simply pass a set of parameters to represent the deck. IE
java MyClass A B C D
and it'll shuffle them until a shuffle results in the original list again. Note it's non-deterministic since the shuffle is randomised...
import java.util.Random;
public class MyClass {
  private static final Random generator = new Random();
  private Object[] contents;
  public MyClass(Object[] contents) {
    this.contents = (Object[]) contents.clone();
  public void shuffle() {
    for(int j = 0, k = contents.length; j < k; j++) {
      int r = generator.nextInt(k);
      Object o = contents[j];
      contents[j] = contents[r];
      contents[r] = o;
  public int getSize() {
    return contents.length;
  public Object get(int index) {
    return contents[index];
  public boolean equals(Object object) {
    boolean result;
    if(object instanceof MyClass) {
      result = equals((MyClass) object);
    else if(object instanceof Object[]) {
      result = equals((Object[]) object);
    else result = false;
    return result;
  private boolean equals(MyClass object) {
    boolean result;
    if(getSize() == object.getSize()) {
      result = true;
      for(int j = 0, k = getSize(); j < k; j++) {
        Object o1 = get(j);
        Object o2 = object.get(j);
        if(o1 != o2 && (o1 == null || !o1.equals(o2))) {
          result = false;
          break;
    else result = false;
    return result;
  private boolean equals(Object[] object) {
    boolean result;
    if(getSize() == object.length) {
      result = true;
      for(int j = 0, k = getSize(); j < k; j++) {
        Object o1 = get(j);
        Object o2 = object[j];
        if(o1 != o2 && (o1 == null || !o1.equals(o2))) {
          result = false;
          break;
    else result = false;
    return result;
  public String toString() {
    String result;
    if(contents.length == 0) result = "";
    else {
      StringBuffer sb = new StringBuffer();
      sb.append(contents[0]);
      for(int j = 1; j < contents.length; j++) {
        sb.append(", ").append(contents[j]);
      result = sb.toString();
    return result;
  public static void main(String[] args) {
    MyClass shuffler = new MyClass(args);
    System.out.println(shuffler);
    int j = 0;
    do {
      shuffler.shuffle();
      j++;
    } while(!shuffler.equals(args));
    System.out.print(++j);
    System.out.println(" shuffles before returning to original.");
}Hope this helps
PS: Don't forget the dukes in this and the other thread - you can't get 'em back so you might as well reward people...
Talden

Similar Messages

  • Creative Zen 8GB Random Shuffle

    Creative Zen 8GB Random Shufflej I have owned a Creative Zen V Plus 4GB for two years and have really enjoyed it. About a month ago, the screen started to fade out and is now totally black. I have researched this problem and have come to the realization that there is no way to fix the issue short of either shipping my player in for repairs or buying a new Zen V Plus. I have tried all the "fixes" I could find on here and other places. Nothing works and a lot of others seem to have the same issues with their players. The player still plays fine, but with no screen, it is very?tough to use correctly. I am willing to "start over" with a new player and am interested in the Creative Zen 8GB. The player looks real nice and has all the features I need. I read online that the Creative Zen 8GB DOES NOT have a random shuffle feature. I was just wondering if this is true? I love setting my player on random shuffle and letting it randomly choose the songs. If this is not a feature on the Zen, I guess I wouldn't be interested in buying one. Does anyone know if the Creative Zen 8GB has the random shuffle feature?

    Yes it does. You would from the music menu scroll to DJ & choose random play all

  • Random shuffle no longer works

    I have itunes installed on my pc.  Random shuffle feature no longer works. Latest version on itunes is installed.

    Shuffle mode displays songs in the wrong order when you have the song number field selected in the first column.  My iTunes displays a shuffled playlist in the order in which songs are added to library, not in the playlist order for shuffle mode.
    Songs are not in the order in which they will play, shown above.  They are in Date Added order in shuffle mode with song number selected as the sort order in iTunes.  This IS an iTunes problem.  Not an issue in iTunes 10 or previous.

  • How do I get random shuffle to work in an album?

    How do I get random shuffle to work in an album and why am I unable to turn off the music Photos selects? I used to listen to my iTunes playlist while I was watching an album in random shuffle mode.

    There is no random shuffle in Photos.
    http://www.apple.com/feedback/macosx.html is the place for feature requests and feedback

  • Can I used the 'Random' shuffle button on CS6 with a standard DVD (blu-ray/web?)

    Hi guys,
    I'm just wondering if I can use the random shuffle button support on CS6 with a standard DVD?
    Thanks,
    Praga

    No... not on a DVD that you play in a player attached to a TV... from the user guide PDF
    Chapter PlayList support for Blu-ray and web DVDs
    Encore CS6 includes new options that provide more flexibility when you play chapter playlists:
    Sequential: Play chapter playlists sequentially.
    Random: Let the player randomly select a set of chapters to play.
    Shuffle: Let the player play all the chapters in any random order.
    Encore CS6 provides only the Sequential option for chapter playlists of DVD projects. Find these options in the Chapter PlayList panel. In addition,
    Encore CS6 supports Random and Shuffle options for chapter playlists in web DVDs.

  • My iphone 4S will randomly shuffle my playlists regardless of what order I put them in.  It also just removes songs at random.  What is going on???

    My  new iphone  4S is not keeping playlists in th order I sync them and will randomly shuffle them no matter how many times i rearrange them.  I don't think I have the shuffle setting on, but can't figure out why it shuffles, removes, and adds songs at will.  I am a Zumba instructor and need my playlists in a specific order.
    Thanks,
    k

    I'm having a similar problem with my iPhone 4s.  Just loaded all of my play lists onto it and it shuffles the order on some playlists but not others.  It is not a matter of shuffle being selected anywhere.  In fact when I connect my iPhone to my Mac and look my play lists in iTunes, they appear in the correct order.  When I view my playlists on my iPhone, some appear in a random order.  The suggestions to turn shuffle off have not helped since nowhere does shuffle appear to be on.  And I have the "shake to shuffle" turned off as well.  I listen to a lot of continuously mixed music so these randomizations of my playlists are very problematic.  I didn't have this problem at all with my iPod Touch. Should I just try deleting the troublesome playlists and reinstalling them?

  • Playing unchecked tunes during random shuffle. Is it possible?

    Hello,
    I was wondering if there is a way to have unchecked tunes in my library play when I have a random shuffle going--while still keeping them unchecked. Specifically, I want to keep unchecked songs off of my iPod, but I still want to be able to play them on iTunes on my computer during a random shuffle.
    Thanks.
    -V

    No, unchecked songs won't play during random shuffle. They will only play if you specifically select them and press Play.
    My advice would be to stop using checked and unchecked as the (exclusive) way to determine what goes on your iPod. It's quite cumbersome. I used to do it that way. Now, I use "Sync selected playlists". It's much more flexible and then you don't have the problem that led you to post. One simple way to do it is to create a playlist that's the right size for your iPod. For example, if you have a 4 gig Nano, you might have a playlist that selects 3 gigs of music least recently played, rating 3 or above, live updating. Then, just sync that playlist. I also make playlists of things I specifically want to hear, like the last couple of albums I bought. It's worth thinking about.

  • Does iTunes 10.5.2 allow you to control how random shuffle play acts?

    I seem to remember a previous version of iTunes that allowed you to adjust the randomness of shuffle-play.  There was a slide bar that you could move, and I believe that it was based on the songs you most listened to.
    Does the current version still have that function?  I can't seem to find it, and my shuffle-play doesn't seem to be as random at the moment.  My impression is that I'm hearing more the songs that have a high play count already than not.

    I have this problem too, and I want to make random shuffles such that play counts for each song is uniformly distributed. But how come nobody even notice this thread?

  • Random Shuffle Play

    New iPOD user! My old .mp3 player had a random shuffle play. Basically, it would randomly play songs within a playlist. I've figured out how to shuffle a playlist, but that is a one time shuffle...not a random play. Is there a way to play songs in a random order within a playlist?
    Thanks!

    I didn't have my iPod with me when I wrote that, but when I try that, it still works for me.
    First of all, you have the "Shuffle" under Settings set for "Songs", correct?
    Then, navigate to Music > Playlists. You will se one or more playlists that you've synced to your iPod.
    Highlight (with the clickwheel) the playlist that you want to play with shuffled songs. Do not click on the playlist's name, but simply highlight it as if you were going to click on it.
    Then, clicking on the Play/pause button on your iPod, you should begin to play that playlist's content that you've highlighted, just in shuffled order.
    Doing that exact method, it works for me. Does it work and shuffle the playlist's content for you when you try that?
    -Kylene

  • Random shuffling

    Today my iPod started shuffling tracks within a playlist. It shuffles music and audiobooks. The setting is not on shuffle so I'm not sure where this random shuffling is coming from. Any ideas? Help?

    So the Shuffle setting that is accessible via the Now Playing screen is white (meaning it is off).  The Shuffle icon looks like to arrows crossing over each other and can accessed by tapping the Now Playing screen once, which also brings up the repeat icon/setting and scrubber bar.
    B-rock

  • My iPod Nano no longer randomly shuffles

    All of a sudden, my iPod Nano no longer moves randomly to the next song, but repeats the same song over and over.  I have to manually "shuffle."  The shuffle indicator does not seem to activate.  Any advice?

    sound, like it needs repaired
    but 1st try an restore to factory setting's plug the ipod in to computer and click on the ipod tab click on restore, if its still the same as before it's time for repair check out this web site
    www.ipod-surgery.co.uk

  • Frame Random Shuffle without repeating

    Hi,
    Am beginning an eLearning project (I'm a retired social worker aiming in this lesson to help others) which includes building in AS3 an exercise called the GNAT (Go No/Go Association Task). The GNAT is a test tool that measures people's subconscious attiudes by having them choose (or not choose) by  pressing/touching the spacebar (in this lesson) when they see a specific target word.
    For the first part of the GNAT, a practice exercise: 16 target words (presented one each in its own frame), taken randomly with no repeats from a list or array of 18 words. Frame needs to time out in 1000 ms. If the person reacts by pressing/touching the spacebar when they see a word, it is either scored +1 (depends on the word) or nothing. If pressed, an X or 0 displays for 100 ms (wrong or correct, depending on the word) and then goes immediately to the next random (no repeat) frame and word. If not pressed the timer timeout at 1000 ms goes to the next random (no repeat) frame.
    Whew! For my relatively modest AS3 skills, not sure how to set up a model for this and implement in script. Could also be one dynamic text box in a single frame instead of multiple frames, with the text (target word) changing. But still would need a timer to advance and if pressed, an over-ride of the timer to advance, plus scoring.
    If I can get a basic model working, then I can try to build the main test exercise tool which has 9 target words but these are recycled randomly in a 3:6:3 ratio totaling 70.
    Major challenge!!!
    I've tried various random no repeats using frames but can't get the most basic version to work.
    I would appreciate any help getting started. How best to set this up and begin?
    Regards,
    saratogacoach

    Using frames is not likely to be relevant to what you seem to want to do.  One frame, one textfield, and code is all you should need.
    Just create the array, shuffle it, and pick the first 16 or whatever words in the shuffled array, one at a time as you advance thru the exercise.  If you want a function to shuffle the array you can find the same one often offered in this forum, such as here...
    http://forums.adobe.com/thread/1058786?tstart=0
    If you need help with setting up a Timer, you should not have a problem searching Google for a tutorial (use "AS3 Timer tutorial")

  • Why is Random/Shuffle playing only a limited subset of songs?

    For no reason that I can discern, the Random function is playing only from a small number of songs -- mainly ones I added today and yesterday. I noticed this because I have a custom "Shuffle" playlist that I've used for years and today I dumped about 125 new songs into my music library. All I was hearing was the new songs. When I quit the playlist and went directly to my library, with Random still selected, no matter what song I started with the queue would still contain only the new songs.
    When I turn the random function off everything plays in the order it should.
    I have a huge library -- over 25,000 files. Have I hit the limit that Random can deal with? (Just kidding, I don't really think that's it.)
    I've tried trashing com.apple.iTunes.plist in Preferences and restarting the computer, which made no difference. I've turned off the Remote app on my iPad, which I often use to control playback. Beyond that I'm stymied.
    Cary

    As a follow-up, I tried reinstalling iTunes. No change. Then I tried editing my playlist so that it didn't include any songs added after last month, which resulted in it coming to a grinding halt after playing whatever song I selected.
    Now, in fact, when I try to play anything from a playlist I can only play one song. Clicking on the "Up Next" queue brings up the message "No upcoming songs."
    Curiouser ...

  • My iphone loves to watch Beyonce dance (randomly shuffling to videos)

    I've got a couple of questions about strange iphone behavior. Its a 3G S, and these oddities are different than my first generation iphone.
    First of all, it seems like when I use voice control to tell the iphone to "play album __________" then the default behavior is to shuffle the songs, not play them in sequence. Is that changeable?
    Secondly, this is really bizarre, but related. I keep trying to listen to Beyonce's "I Am...Sasha Fierce" album straight through after downloading it directly from the phone, but it keeps restarting and playing the videos that came with the album instead of playing the songs straight through. the only explanation that I can think of is that my iphone really likes to watch Beyonce dance. Anybody have a better idea? Kidding aside, it's kind of frustrating.

    This is my home Wi-Fi. WPA Protected. I just ran into another problem just now... I was trying to reconnect to my Wifi and then it asked me for a password. I had my password stored in it so I do not know why it was asking me now. So when I put in the password it says incorrect password. I tried it 10 times and I know it was the right password and it says incorrect password. Just now though when I try to put in my password and I press join it just jumps back to password input. I looked at the IP and it turns out that all my IP settings that were supposed to be there are gone.
    My computer can connect to the router perfectly fine.
    Also, could you tell me how to update the firmware?
    The randomly turning off only happens with this router but I am not sure about not being able to connect.

  • Playlist - songs - randomly (shuffle)

    Could someone please inform me about how I can shuffle songs within a playlist on an iPod-Classic? F.i. I have 100(0) songs on a playlist and - as you can imagine - I want to listen to them randomly. What do I have to do for this?
    Thanks
    G

    If you have already started the Playlist running, press the select (centre) button three time so that the "shuffle" options appear. Use the scroll wheel to move to either "songs" or "albums" and press the select button to set it.
    To turn on the Shufflle feature and begin the Playlist with a random song, go to Settings/Shuffle and press the select button to change between "off, songs, albums". Then simply go to and highlight the Playlist and press the Play button (without going into the Playlist). A (random) song from that Playlist will start playing and the total number of songs (X) in that Playlist will be shown in the Now Playing screen as 1 of X.

  • Random Shuffle

    I just got a new battery, and now my ipod shuffles randomly - it will play normally for a while but if I choose to skip a song, it will skip more than the current song - sometimes it will skip 3 - 5 songs but it's inconsistant. sometimes i can skip a song fine...anyone ever had this problem?
    mini   Windows XP  

    Welcome to Apple Discussions!
    Try going through the Five Rs.
    Also, were these songs purchased from the iTunes Store?
    btabz

Maybe you are looking for

  • Error while accessing portal via internet

    Hi all, I am having problems when accessing portal through internet. I have installed NW04 SR1(WAS, EP & KM). Then I created some iviews and pages. Now <u>when I open the portal through internet(using its ip address)</u> and try to access PCD, it is

  • Deposit on sales order can't include tax??

    I recently posted a thread on prepaid invoices and thought I had my problem resolved.  However after a little more testing I realized that B1 is not allowing me to enter a deposit amount for the full invoice amount (Total Before Discount and Tax amou

  • Which dual monitor for iMac 21.5" 2010?

    Hey guys, I've had my iMac 21.5" 2010 model for a while now, but now I would like another monitor. It must be: -Around 21.5" -Preffibly LED back lit -Black and/or aluminium -Connect to my iMac Do any of you have any suggestions for good dual monitors

  • Firefox 4 hangs when removing attachments from Outlook Express email

    When I open an email in Outlook Express 6 (Windows XP Pro) and try to drag & drop an attachment to the desktop with Firefox 4 open in the background, my computer hangs. This only happens when FF4 is open - I have no problem removing attachments with

  • Aperture is Exporting but I don't know what its exporting...

    On the bottom the wheel is spinning saying that its Exporting. I didn't instruct any export and when I click to see what its exporting, there is nothing in the activity window and no way to clear the export... anyone have any idea what this is about?