mardi 2 août 2022

Making Sure a Number Isn't "Guessed" Twice?

I have the following code that sets an initial random number, and random numbers are generated until a random number matches the initial random number - I also record how many guesses it took for this to happen (and then repeat this process many times over - each repetition is called a "game"):

all_games <- vector("list", 100)

for (i in 1:100){
    guess_i = 0
    correct_i = sample(1:100, 1)
    trial_index <- 1  
    while(guess_i != correct_i){
        guess_i = sample(1:100, 1)
        trial_index <- trial_index + 1  
    }
    
    game_results_i <- data.frame(i, trial_index, guess_i, correct_i)
    all_games[[i]] <-  game_results_i
}
  • Is it possible to modify this code to ensure that in any game, no number is guessed twice?

I thought that maybe I could ensure this by keeping track of all the numbers that were guessed in a game, and then removing them from the possible numbers that could be generated in the next turn:

all_games <- vector("list", 100)

guesses_in_a_game <- list()
all_guesses <- list()

for (i in 1:100){
    guess_i = 0
    correct_i = sample(1:100, 1)
    trial_index <- 1  
    while(guess_i != correct_i){

        guess_i = sample(1:100, 1)
         guesses_in_a_game[[i]] = guess_i
        trial_index <- trial_index + 1  
    }
    all_guesses[[i]] <- guess_i
    game_results_i <- data.frame(i, trial_index, guess_i, correct_i)
    all_games[[i]] <-  game_results_i
} 

But I am not sure how to write the code for this.

Can someone please help me with this?

Thank you!




Aucun commentaire:

Enregistrer un commentaire