3

I'm using Jfugue in Eclipse, and I have a list of music strings in the code. When I run the code, it plays back all of them, but I want to play back one at a time in random order.

So far I'm using:

Pattern pattern = new Pattern ("A");
Player.play(A);
Pattern pattern = new Pattern ("B");
Player.play(B);

I've tried using "Random random = new Random();" But this has not been working, and I don't know how to implement it.

I've also tried re-suing a random word generator:

  for(int i = 0; i < numberOfTest; i++) {
    int index = (int)(Math.random() * 10);
    System.out.println(strings[index]);

But I don't know how to replace the word strings with music strings:

In general most of my problems stem from a lack of familiarity with the correct syntax, especially Jfugue.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • Hints: A) you should follow java naming conventions. I assume that "Player" is a (local) variable, so you should name it *p*layer (variable names go camelCase in Java B) only use tags that are relevant for your question. The type of IDE or editor you use ... isn't at all relevant for this question. – GhostCat May 09 '19 at 05:45

1 Answers1

2

Put them into a list, and then shuffle that:

List<Pattern> allPatterns = Arrays.asList(new Pattern ("A"), new Pattern ("B"), ... more patterns);
Collections.shuffle(allPatterns);

And please note: the above is plain and simple java, it works independently of any specific framework such as jfugue.

For playing, you simply have to tell the player to play the patterns using the order of your shuffled list:

// by using the for-each loop
for (Pattern onePattern : allPatterns) {
   player.play(onePattern);
}

// alternatively, turn the list back into an array and play that
player.play(allPatterns.toArray(new Pattern[0]);
GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • @WolfgangAmadeusMozart See my updates please. Assuming that the player plays "patterns"; of course you have to tell the player to use the order from that random list you put together! – GhostCat May 09 '19 at 05:39
  • @WolfgangAmadeusMozart I am not an expert on jfugue, thus I can't tell you the difference between: A) calling play() multiple times within a loop, and B) calling it once, passing an array of values. Both solutions, in the end, play the same "set" of patterns. But A) calls the method multiple times, and B) calls it only once. – GhostCat May 09 '19 at 14:48
  • @WolfgangAmadeusMozart And to answer your other question: what I showed you ... should play a random order each time. When you call shuffle(), and you have enough elements in that list ... you should get a different order each time. So, if your **current** code isn't doing what you want to ... please write up a new question, include the current code, and explain what happens there, and how it is unexpected. Please avoid using comments to come back with more and more questions. That is not how this site intends things to happen. – GhostCat May 09 '19 at 14:49