making random arrays more random

I am working on an app that displays random trivia questions in the form of an array, but they tend to keep repeating. Is there a way to make them more random?
Answered by OOPer in 618404022
So, you do not want to show the same question repeatedly. Then something like this would be very near to what you want.
Code Block
    let array = [
        "The first astronaut to step on the Moon was John Glenn",
        "The largest continent is Australia",
        "The Chicago Cubs won the World Series in 2015",
        "Delaware was the first state in the modern United States",
        "Alaska was the last state in the modern United States",
        //...
    ]
    var unusedQuestions: [String] = []
    @IBAction func questButton(_ sender: Any) {
        if unusedQuestions.isEmpty {
            unusedQuestions = array.shuffled()
        }
        let nextQuestion = unusedQuestions.popLast()!
        questLabel.text = nextQuestion
    }


Please show your current code.

How are you making your random arrays? The original questions are kept in another Array? Are you using some random method of Swift Array?

The solution depends on all such things, including the definition of your more random.
This is the code I used to get the user to generate the random questions the user is asked to answer:

   @IBAction func questButton(_ sender: Any) {

        let array = [" my various questions are listed here"]

(example of the questions......"The first astronaut to step on the Moon was John Glenn", "The largest continent is Australia", "The Chicago Cubs won the World Series in 2015", "Delaware was the first state in the modern United States", "Alaska was the last state in the modern United States")

        

   questLabel.text = array.randomElement()

  }

I have a 90 second timer built into the app, I want the user to see how many they can answer "True" or "False" in that time. The app is supposed to display a different question every time the user clicks on the "questButton" but it tends to repeat them frequently. I have a list of about 60 plus questions, but when I test it I can get 5 or 6 questions that keep popping up, and they are not always the same questions.
Because it is tracking the CORRECT or WRONG answers, the fact that they repeat makes it easy for the user to get the same question correct over and over again.


Accepted Answer
So, you do not want to show the same question repeatedly. Then something like this would be very near to what you want.
Code Block
    let array = [
        "The first astronaut to step on the Moon was John Glenn",
        "The largest continent is Australia",
        "The Chicago Cubs won the World Series in 2015",
        "Delaware was the first state in the modern United States",
        "Alaska was the last state in the modern United States",
        //...
    ]
    var unusedQuestions: [String] = []
    @IBAction func questButton(_ sender: Any) {
        if unusedQuestions.isEmpty {
            unusedQuestions = array.shuffled()
        }
        let nextQuestion = unusedQuestions.popLast()!
        questLabel.text = nextQuestion
    }


I just keyed in your suggestion and ran the app.....it is much better than it was THANK YOU. I have 65 questions so far, and in my test in the first 25 question it repeated 3 questions, but my original version was almost triple that.
When I retired I took up trying to write apps as a dare for my grandkids so I am just short of a "newbie". I hope some day I can look at code and understand it and be able to help someone the way you have helped me.
I really can't tell you how much I appreciate your help.....THANK YOU so much for the help and being so quick to respond!!


jmroz51
making random arrays more random
 
 
Q