-1

I have array of few random numbers ( I do not know which ones it's going to be and how many of them, but it's mainly in range from 1 to 15, should not be more )

How to add new random number to this which should be as close as possible to this range and which will be unique ( can not be the same as one of the existing numbers )?

It's in javascript, example array:

var myarray = [4,5,1,9,6];
Zack
  • 106
  • 1
  • 8
  • Can you show us your code so far? What didn't work, how did you attempt to solve the problem? – elclanrs Apr 16 '14 at 23:28
  • 1
    You're going to have to define "as close as possible." To check if an item is in an array, just do `if ( myarray.indexOf( someNum ) !== -1 ) { //do something }` – user1091949 Apr 16 '14 at 23:29
  • @elclanrs i got stuck in 2 loops I want to see if there is better solution – Zack Apr 16 '14 at 23:30
  • Simply put all your numbers in row and using an http://en.wikipedia.org/wiki/Fisher-Yates_shuffle#The_modern_algorithm technique you'll get all 15 pretty shuffled – Roko C. Buljan Apr 16 '14 at 23:30
  • So instead of putting numbers one by one and hitting hard on algorithms, loops and stuff... http://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array, and you just pick your desired 'random' number out of the shuffled Array. – Roko C. Buljan Apr 16 '14 at 23:34

1 Answers1

1

A function like this should work:

function addRandom(array, max)
{
    if(array.length === max) {
        return array;
    }

    var number = Math.round(Math.random() * (max - 1)) + 1;
    if(array.indexOf(number) === -1) {
        array.push(number);
        return array;
    }

    return addRandom(array, max);
}

This takes your starting array and a maximum random integer. If the length of the array is already equal to the max integer, then the array is full and you won't ever find a new one. Otherwise, we make a new random number between 1 and the maximum.

To do this, we use Math.random() (which returns a float from 0-1), multiply it by one less the maximum, round the result with Math.round(), and add 1. If you say our max is 15, then this takes a float from 0 to 1 and multiplies it by 14 so you get 0 to 14. This is then rounded to an integer and we add 1 so we have a range of 1 to 15.

Finally, we use Array.indexOf() (IE 9+) to see if the number already exists. If it doesn't, then we use Array.push() to append the number and then return the new array. Otherwise, we will run the function again and generate a new number.

Use my JSFiddle to test this. I start with an array with length 5, and loop through it 15 times. You will see in the console that a random number is appended each time until every number is generated. An example out put is:

[4, 5, 1, 9, 6, 8]
[4, 5, 1, 9, 6, 8, 15]
[4, 5, 1, 9, 6, 8, 15, 2]
[4, 5, 1, 9, 6, 8, 15, 2, 3]
[4, 5, 1, 9, 6, 8, 15, 2, 3, 12]
[4, 5, 1, 9, 6, 8, 15, 2, 3, 12, 14]
[4, 5, 1, 9, 6, 8, 15, 2, 3, 12, 14, 7]
[4, 5, 1, 9, 6, 8, 15, 2, 3, 12, 14, 7, 11]
[4, 5, 1, 9, 6, 8, 15, 2, 3, 12, 14, 7, 11, 10]
[4, 5, 1, 9, 6, 8, 15, 2, 3, 12, 14, 7, 11, 10, 13]
[4, 5, 1, 9, 6, 8, 15, 2, 3, 12, 14, 7, 11, 10, 13]
[...]
Sam
  • 20,096
  • 2
  • 45
  • 71