Random words from text file - how to avoid duplicate index numbers?

Hey.
I am quite new to processing so excuse me if my explanation of this is in the wrong terms or if things are slightly misunderstood.

I am working on a code that write random words from a text on the screen every time the mouse is clicked. What I want it to do in the end is to use ALL the words randomly from the text, and when all the words is used, the whole process should start over from a blank screen. These two last steps I haven’t been able to implement. So far the code takes random words from the text and put’s them on the screen, but I have no guarantee that the same word won’t get repeated(of course the same word can appear as many times as it appears in the text). So as I see it, I want to avoid the same index number from the array being used more than one time. I also want everything to reset when the last index number is used.

To summarize what I want to implement:

  1. I want all the index numbers of the array used without having any duplicates.
  2. When all the index numbers is used, I want everything to start over from a blank screen.

Hope you understand.

Here is my code:

String[] words;
boolean newWord = false;
float x = 25;
float y = 40;

void setup() {
  fullScreen();
  background(0);
}

void draw() {


  String[] lines = loadStrings("Skabelsesberetningen.txt");
  String  entireplay = join(lines, " ");
  entireplay = entireplay.toLowerCase();
  printArray(entireplay);
  words = split(entireplay, " ");
  words = splitTokens(entireplay, ":!?/()| "); 

  println(entireplay);

  noLoop();

  PFont f = createFont("Foundry Flek Grid", 18);
  int index = int(random(words.length)); 
  if (newWord) {
    textFont(f);
    text(words[index], x, y);

    x += textWidth(words[index] + " ");
  }

  if (x > width-85) {
    x = 25;
    y += 20;
    
  }
}

void mousePressed() {
  loop();
  newWord = true;
}

Any suggestions?

Thanks, Jonathan

1 Like

Hi Jonathan,

Welcome to the forum.

You could create a list of numbers (indices) and shuffle them. Go through them all and start over. If you find/create a good mechanism to shuffle the list you need to create list of indices only once and shuffle it again once list has been gone over.

Or you could have a list or queue of indices, randomly pick one of them, remove it from the list (so you cannot pick it again), use the corresponding word and pick next.

Looks like ArrayList class has a shuffle()-function so it’s probably easiest way forward. https://www.geeksforgeeks.org/collections-shuffle-java-examples/ has even code example how to do it.

2 Likes
1 Like

Heh, I didn’t know about intList even less that it has shuffle. So there’s no need for ArrayList in this case.

Given it’s a String[], it can be converted to a StringList very easily: :bulb:

Of course, a StringList container got method shuffle() as well: :wink:

1 Like

Hey!

Thank you very much for your answers! It seems like converting the string to a StringList and use the shuffle function would be the easies way now that the text file is a string. And then find a way to make the whole process repeat. I will dig into this and hopefully make it work. Very nice with some help :slightly_smiling_face:

1 Like