0

My exact requirement is to return all numbers from 1 to 2000 randomly and shouldn't repeat a number twice. So we can say if i have

function generateRnNumber(){
  var arr = [1,2,3,4,5,6,7,8,9,10,...2000];
  randomNumber = Math.floor((Math.random()*2000));
  return randomNUmber;
}

So if i call generateRnNumber a number between 1 - 2000 which is not returned before, or a unique number so if i call 2000 times i should get all the numbers but in random order. I don't want to keep an array with 2000 elements. Please help me in writing this function.

gintech
  • 303
  • 1
  • 5
  • 10

2 Answers2

-1

Keep an array with all possible values and randomize the order. When you need a random value, pop the array (removing the value the "possible values"-array).

To do this otherwise, you would anyway need an array to hold the "taken" values so i don't think you can get around keeping an array of the size of your "value range".

tonsteri
  • 683
  • 7
  • 15
  • As i mentioned, its 2000 numbers, so i don't want to keep an array with 2000 elements, if you can help with some other methods. – gintech Oct 14 '14 at 11:59
  • 1
    @RahulUkil first of all, 2000 integers isn't that much data, and if you want complete uniqueness, and have no number return twice, then you need to know what you have already returned. I agree with Tonsteri, it's a simple problem with a simple solution. – André Snede Oct 14 '14 at 13:02
-1

You can shuffle the array like this:

function shuffle(o){
    for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
    return o;
};

usage:

var myArray = [];
for(var i = 1; i <= 2000; i++) {
    myArray.push(i);
}
var randomArray = shuffle(myArray).splice(0, 10); // an array with 10 random numbers varrying from 1-2000

source: How can I shuffle an array?

Community
  • 1
  • 1
devqon
  • 13,818
  • 2
  • 30
  • 45
  • 1
    Instead of basically copying a answer from a different SO question, Closevote as a duplicate, instead. – Cerbrus Oct 14 '14 at 12:01
  • @Cerbrus marking it duplicate and referring to a thread where the question and answer both are different. Can u please go through my queries again. Or do u think its okay to write an array with 2000 elements in it and then call shuffle?? – gintech Oct 14 '14 at 12:16
  • @andrew What he did ? marked as duplicate . and pointed the link to another duplicated SO question !!!! How can i shuffle an array in JavaScript? [duplicate] – Mohammad Sadiq Shaikh Oct 14 '14 at 12:18
  • @sadiqxs: I can't "Close vote" on Javascript tagged questions, because I have a gold badge in that tag. All my close votes on [javascript] questions are binding. They just close the question. – Cerbrus Oct 14 '14 at 12:19
  • @andrew are you people suggesting to create an array with 2000 elements??? and then run the script? – gintech Oct 14 '14 at 12:19
  • @RahulUkil: That would be an option. however, if it's always a array of numbers, I'd do it like this: 1: create an empty array. 2: In a loop: (Generate a random number between 0-2000, if it's not in the array yet, add it); loop until the array is of the desired length. – Cerbrus Oct 14 '14 at 12:21
  • 1
    @RahulUkil what you need to do is `shuffledArray.slice(0,10)` that will give you 10 random numbers – andrew Oct 14 '14 at 12:22
  • see edited answer, which answers the question and is not the question which is marked as duplicate – devqon Oct 14 '14 at 12:24
  • @Cerbrus yes it is all numbers. Going by with your suggested method everytime when the page is loaded this loop will run for 2000 times to complete the array. is that okay??? – gintech Oct 14 '14 at 12:29
  • @devqon what you mean by `which answers the question and is not the question which is marked as duplicate `?? – Mohammad Sadiq Shaikh Oct 14 '14 at 12:30
  • @devqon i need all the 2000 numbers between 1-2000, in random order not only 10 numbers. – gintech Oct 14 '14 at 12:31
  • Then you don't need to do the splicing. @sadiqxs his question was also how to create the array and how to get a number of random elements. – devqon Oct 14 '14 at 12:32
  • @RahulUkil: In that case, just build a array of 2000 items, like you're already doing, and shuffle it using the function in the question I linked. – Cerbrus Oct 14 '14 at 12:34
  • @cerbrus thanks. so on loading this page and everytime creating an array with 2000 items and then reading this 2000 items is okay?? – gintech Oct 14 '14 at 12:38
  • An array of 2000 numbers is peanuts for JS. It's no problem. – Cerbrus Oct 14 '14 at 12:38
  • Oh thanks a lot @Cerbrus will write a code with your method. Thanks :) – gintech Oct 14 '14 at 12:49