How to assign random integers to a matrix

Suppose I wish to create a 10x8 matrix with non-repeated random integers between and inclusive of 1 and 8, I used
a <- matrix(sample(1:8, size=8, replace=F), nrow=10, ncol=8)
but this gives me repeated sequences, such as "3, 4, 7, 8, 3, 4, 7, 8" for row #1 below:
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 3 4 7 8 3 4 7 8
[2,] 2 1 6 5 2 1 6 5
[3,] 4 7 8 3 4 7 8 3
[4,] 1 6 5 2 1 6 5 2
[5,] 7 8 3 4 7 8 3 4
[6,] 6 5 2 1 6 5 2 1
[7,] 8 3 4 7 8 3 4 7
[8,] 5 2 1 6 5 2 1 6
[9,] 3 4 7 8 3 4 7 8
[10,] 2 1 6 5 2 1 6 5

Hi, and welcome!

Your code snippet is all that's required in this case. For anything data dependent or more complicated, however, please use a reproducible example, called a reprex.

One way to generate random integers is

(my_randoms <- sample(1:10, 8, replace=T))
#> [1] 4 5 9 6 1 9 1 2

Created on 2020-01-06 by the reprex package (v0.3.0)

Many thanks!

Now I have a new problem:
nCols <- 8
nRows <- 10
NegFitness <- c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
Fitness <- c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
EightQueenSets <- matrix(0, nrow=nRows, ncol=nCols)
for (k in 1:nRows){
EQS1<- sample(1:nCols, size=nCols, replace=F)
EightQueenSets[k,]<-EQS1
}

Compute Fitness of Each EightQueen Set

Maximum Fitness = 8

Fitness = 8 - Negative Fitness

for (k in 1:nRows){
for (i in 1:nCols-1){
for (j in i+1:nCols){
if (EightQueenSets[k,i]==EightQueenSets[k,j]){
NegFitness[k] <- NegFitness[k] + 1
}
if ((EightQueenSets[k,j]-EightQueenSets[k,i])==(j-i)){
NegFitness[k] <- NegFitness[k] + 1
}
if ((EightQueenSets[k,j]-EightQueenSets[k,j])==-(j-i)){
NegFitness[k] <- NegFitness[k] + 1
}
}
}
Fitness[k] = 8 - NegFitness[k]
}

The error:
Error in if (EightQueenSets[k, i] == EightQueenSets[k, j]) { :
argument is of length zero

Everything nopw works, except a new error appears:

Generate Population of Size 100

nCols <- 8
nRows <- 100
for (k in 1:nRows){
NegFitness[k] <- 0
Fitness[k] <- 0
}

EightQueenSets <- matrix(0, nrow=nRows, ncol=nCols)
for (k in 1:nRows){
EQS1<- sample(1:nCols, size=nCols, replace=F)
EightQueenSets[k,]<-EQS1
}

for (k in 1:nRows){
for (i in 1:(nCols-1)){
for (j in (i+1):nCols){
if (EightQueenSets[k,j]-EightQueenSets[k,i] == j-i) NegFitness[k] <- 1+NegFitness[k]
if ((EightQueenSets[k,j] - EightQueenSets[k,i]) == -(j-i)) NegFitness[k] <- 1+NegFitness[k]
}
}
Fitness[k] <- 8 - NegFitness[k]
}

The Error:

The error:
Error: object 'NegFitness' not found

Thank you guys!

The code now works without errors:
closeAllConnections()
rm(list=ls())

Generate Population of Size 100

nCols <- 8
nRows <- 100
NegFitness = rep(0, nRows)
Fitness = rep(0, nRows);

EightQueenSets <- matrix(0, nrow=nRows, ncol=nCols)
for (k in 1:nRows){
EQS1<- sample(1:nCols, size=nCols, replace=F)
EightQueenSets[k,]<-EQS1
}

Compute Fitness of Each EightQueen Set

for (k in 1:nRows){
for (i in 1:(nCols-1)){
for (j in (i+1):nCols){
if (EightQueenSets[k,j]-EightQueenSets[k,i] == j-i) NegFitness[k] <- 1+NegFitness[k]
if ((EightQueenSets[k,j] - EightQueenSets[k,i]) == -(j-i)) NegFitness[k] <- 1+NegFitness[k]
}
}
Fitness[k] <- 8 - NegFitness[k]
}

1 Like

Great. Please mark the solution for the benefit of those to follow.

Thanks!

I'll mark it as the solution after I completed the code with the full solution.
The next part is to select the EightQueenSets with highest fitness to generate a new "population"
to see if we can get more solutions to the EightQueens Problem.

On account of the title, I decided to mark this thread as complete.
A great appreciation to you guys again!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.