Home > OS >  Making Sure a Number Isn't "Guessed" Twice?
Making Sure a Number Isn't "Guessed" Twice?

Time:08-03

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!

CodePudding user response:

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.

Yes, the logic is correct. This is what we can do to modify your initial code:

all_games <- vector("list", 100)

for (i in 1:100){
    guess_i = 0
    correct_i = sample(1:100, 1)
    guess_sets <- 1:100  ## initialize a set
    trial_index <- 1
    while(guess_i != correct_i){
        guess_i = sample(guess_sets, 1)  ## sample from this set
        guess_sets <- setdiff(guess_sets, guess_i)  ## remove it from the set
        trial_index <- trial_index   1  
    }
    ## no need to store `guess_i`, right? it is as same as `correct_i`
    game_results_i <- data.frame(i, trial_index, guess_i, correct_i)
    all_games[[i]] <- game_results_i
}

In fact, we can replace the inner loop by vectorized code:

all_games <- vector("list", 100)

for (i in 1:100){
    correct_i = sample(1:100, 1)
    guesses <- sample(1:100)  ## a shuffle of 1:100
    trial_index <- which(guesses == correct_i)  ## when the guess is corret
    game_results_i <- data.frame(i, trial_index, correct_i)
    all_games[[i]] <- game_results_i
}

We can continue to simplification (replicate is not vectorized; it is a sugar function):

n <- 100
correct <- sample(1:100, n, replace = TRUE)
guesses <- t(replicate(100, sample(1:100)))
trial_index <- max.col(correct == guesses)
all_games <- data.frame(trial_index, correct = correct)

CodePudding user response:

Put all the guessed numbers in a list or array and then write a check that if the random number is in that list/array then again get a random number.

  • Related