How to create program that randomly picks

edited November 2017 in How To...

Hi everyone,
I'm trying to create a program that can randomly select one word from list. For example, if I have three different boxes with various names in each, every time I press Enter I would like to see names chosen from each box.

Answers

  • What code do you have so far?

    Kf

  • I only tried with colors. But I need help with how to start with this.

  • Answer ✓

    An example below. This code ensures random not-repeated words from your list. If repeated words are allowed, the code would then have the following change: int index=(int)random(strList.length); and you won't need any IntList object.

    Kf

    String[] strList = {"Monday", "Tuesday", "Wednesday", "Th", "Friday"};
    IntList myList=new IntList();
    
    //Creates a sorted list
    for (int i=0; i<strList.length; i++) {
      myList.append(i);
    }
    
    myList.shuffle();  //Shuffled list: Pick the first three items in the list
    
    println("My three random words are:");
    for (int i=0; i<3; i++) {
      int index=myList.get(i);  //Index to use from shuffled list
      println("Item "+i+": "+strList[index]);   //Ensures random not-repeated words
    }
    
    println("\n\nThis is the end.");
    
  • Thank you

  • Hi again, I have a question with same problem. How to present the results from my list? I assume that it's supposed to be inside of a rectangle.

    String[] strList = {"1", "2", "3", "4", "5"}; IntList myList=new IntList(); void setup() { size(640, 600); }

    void draw() {

    for (int i=0; i<strList.length; i++) { myList.append(i); }

    myList.shuffle();

    println("My three random words are:"); for (int i=0; i<3; i++) { int index=myList.get(i);
    println("Item "+i+": "+strList[index]);
    }

    println("\n\nThis is the end.");}

  • Right now you are printing the results out to the console. You can draw them in your sketch window instead by using text().

    size(300,300);
    background(0);
    fill(255);
    text("Words! What are they?", 20, 100);
    
  • I don't understand how to how to show the results from my program. This program should select randomly one from list and show the result.

  • Wait, what are you having trouble with?

    Picking one of the values from your list?

    Or showing text in the sketch window?

Sign In or Register to comment.