How can I create a list of random integer numbers with no two equal?

Juan Carlos Ponce Campuzano shared this question 3 years ago
Answered

Hello everyone,

I know it is possible to create a list of random integer numbers with no two equal, but I have no idea how to do it.

Any suggestions will be appreciated.

Thanks.

Best Answer
photo

With the Shuffle(<List>) you can do nice things. E.g. if you want to have 5 random but no equal numbers from 1 to 20, you can take the first 5 numbers out of the shuffled list of numbers from 1 to 20 with First(Shuffle(Sequence(1, 20)),5).

chris

Comments (3)

photo
1

Ok I found a way to do it using JavaScript in the Global JavaScript.

 


function generate(){
  var arrayContainer = []; // this arrays holds the five random numbers generated;
  
  var genNum = Math.floor(Math.random() * 5) + 1;
  arrayContainer.push(genNum);
  //console.time();
  for (var counter = 1; counter < 5; counter++) {
    //the counter is less than five because we already initialise arrayContainer[0] with genNum
    var newGen = Math.floor(Math.random() * 5)+1;
    while (arrayContainer.lastIndexOf(newGen) !== -1) {
      newGen = Math.floor(Math.random() * 5)+1;
    }
    arrayContainer.push(newGen);
  }
  //console.timeEnd();
  //console.log(arrayContainer);
  return arrayContainer;
  
}

So with this, I can create the lists like {5, 4, 2, 3, 1}, {2, 5, 1, 4, 3}, ...

Any suggestions how to do it with GGBscripting?

photo
1

With the Shuffle(<List>) you can do nice things. E.g. if you want to have 5 random but no equal numbers from 1 to 20, you can take the first 5 numbers out of the shuffled list of numbers from 1 to 20 with First(Shuffle(Sequence(1, 20)),5).

chris

photo
1

Thanks Chris,

This command is great and also easier to use compare to the JavaScript code.

Regards.

photo
© 2023 International GeoGebra Institute