Need to build a new array by randomly picking 18 numbers from another array and also having those same numbers be in the new array twice

get random element from arraylist java
java array
java pick random string from array
how to create an array of random integers in java
java initialize array with values
array of array in java
basic array programs in java
valid array declaration in java

I am making a matching game for a group project in a JavaScript Jquery class. I have 36 images that I can use for the tiles. I want the game to randomly pick 18 of these and put them into a new array. I also need these 18 numbers in the new array twice, because that new array is going to be shuffled and then used the build the game board.

Here is my code:

let objects = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29','30', '31', '32', '33', '34', '35', '36'],

function cardList () {
    var cardList = [36];
    for (i = 0; i < cardList.length; i+=2) {
        cardList[i] = objects[Math.floor(Math.round((Math.random()*36)+1))];
        cardList[i+1] = cardList[i];
    }
    return cardList;
}

I tried to use the Chrome debugger on this and it this isn't creating all the numbers, it's just creating the two initial numbers.

There has got to be a better way to do this, I have looked at several examples of a matching game and most of those have the exact number of cards needs, which may be causing me problems.

Since you (probably) don't want items to appear more than twice in the result, you should choose items without replacement. This is how I would do it:

const objects = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29','30', '31', '32', '33', '34', '35', '36'];
const getList = () => {
  const objs = objects.slice(0);
  const selected = [];
  for (let i = 0; i < 18; i++) {
    const index = Math.floor(Math.random() * objs.length);
    const [num] = objs.splice(index, 1);
    selected.push(num, num);
  }
  return selected;
};
console.log(getList());

Java Code to fill int / double Array with random values. – 5 Balloons, How do you populate an array of random numbers in Java? Contribute your code and comments through Disqus. Previous: Write a Java program to check if a specified array of integers contains 10's or 30's. Next: Write a Java program to create a new array from a given array of integers, new array will contain the elements from the given array before the last element value 10.

To pick the numbers I would simply shuffle the array and take 18 unique elements from it. The best method to shuffle an array is the Fisher-Yates shuffle, below implementation for the shuffle is taken from this answer. All you need to do then is to "concat the array with itself" and you got your starting array with 18 numbers that all exist twice in the array. You could obviously directly shuffle the resulting array then, and voila.

let array = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29','30', '31', '32', '33', '34', '35', '36'];

function buildCardList(array) {
  const shuffled = shuffle(array).slice(0,18);
  return shuffle(shuffled.concat(shuffled));
}


console.log(buildCardList(array));


// Origin: https://stackoverflow.com/a/2450976/3993662
function shuffle(array) {
  var currentIndex = array.length, temporaryValue, randomIndex;

  // While there remain elements to shuffle...
  while (0 !== currentIndex) {

    // Pick a remaining element...
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;

    // And swap it with the current element.
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }

  return array;
}

Excel VBA generate non-duplicated random number, we don't need to cast the double value returned by Math. Randomly selected multiple choices using random.choices() [80, 50, 40, 60, 40] As you can see in the above example we used random.choice s and passed the sampling size i.e., the total number of items that we want to choose from the list randomly. Also, as you can see in the output, we got a few repeated numbers. random.choices() can repeat

See the following working code I made two changes

1) Changed the line var cardList = [36]; to var cardList = Array(36);

2) Changed cardList[i] = objects[Math.floor(Math.round((Math.random()*36)+1))]; to

let index = Math.floor((Math.random()*objects.length));
cardList[i] = objects[index];

And to avoid duplicates I added

objects.splice(index,1);

let objects = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29','30', '31', '32', '33', '34', '35', '36'];

function cardList () {
    var cardList = Array(36);
    for (i = 0; i < cardList.length; i+=2) {
        let index = Math.floor((Math.random()*objects.length));
        cardList[i] = objects[index];
        cardList[i+1] = cardList[i];
        objects.splice(index,1);
    }
    return cardList;
}

console.log(cardList());

How to randomly pick an element from an array, How do you generate a random number from an array? description: generate "number" random numbers from 1 to 100 and store them in positions [0..number-1] So the array is always the same size, but may be filled with a different number of random values on each run void printArray (int array[], int number) parameters : array of integers number of integers stored in the array

Java Concepts: Compatible with Java 5, 6 and 7, How do I make a list of random numbers without duplicates? By problem statement itself, it's very clear that we need to worry about numbers that are adjacent or equal(i.e. absolute difference<=1). freq[] has got frequency of occurance of each element of input array. We just need to return which two adjacent elements appear highest in array.(sum of their frequencies)

Numerical Methods, Algorithms and Tools in C#, How do you generate random numbers in Excel without duplicates VBA? I only need three random numbers within the range of 1-54 (deck of cards) and none of the three numbers can be a duplicate. I cannot use an ArrayList; I must use a standard array. Is the method I chose to check for duplicates good enough?

Pacific Symposium on Biocomputing 2010, Kamuela, Hawaii, USA, 4-8 , public static int getRandom(int[] array) { int rnd = new Random(). to be getting a random element multiple times, you want to make sure your random You can also use Since you have java 8, another solution is to use Stream API. How do I generate random integers within a specific range in Java? MATLAB is an abbreviation for "matrix laboratory." While other programming languages mostly work with numbers one at a time, MATLAB® is designed to operate primarily on whole matrices and arrays. All MATLAB variables are multidimensional arrays, no matter what type of data. A matrix is a two-dimensional array often used for linear algebra.

Comments
  • The way you have this, with "const" in front of getList, means that the array that is getList will be a constant, but the numbers that will be generated each time each time you call getList will be different, correct? I really like JavaScript and I want to make sure I understand the code I am looking at. Thanks
  • @Lisa It's just a way of defining a function - in this case, it's no different from function getList() {, it doesn't imply anything about the return value.
  • This sometimes includes duplicates and also undefineds.
  • This is quite packed with bugs @Lisa Have a look on one of the other answers instead.
  • The problem of undefined is removed. The OP had wrong logic to access the index. That is fixed now. The logic to avoid duplicate is also added