1

I have a function. Every time I call the function, it should return a UNIQUE (for example, if I call this function 90 times, it should have given 90 different numbers) random number under 100.

I am currently doing

var randomNumber = Math.floor(Math.random() * 100);

But, it's not returning unique numbers. It's only returning random numbers.

Thanks in advance.

Edit: It should return some alert after calling 100 times.

Testaccount
  • 2,755
  • 3
  • 22
  • 27
  • 1
    What if you call it 105 times? – Pointy Apr 28 '17 at 19:09
  • It should return some alert after calling 100 times. – Testaccount Apr 28 '17 at 19:10
  • 2
    The usual way to do this is to generate an array of numbers and use a Fisher-Yates shuffle to randomize the list. Then just return elements from the shuffled list sequentially until you run out. – Pointy Apr 28 '17 at 19:11
  • What have you tried? I can't believe you expected that line of code to return unique random numbers... – Chris Apr 28 '17 at 19:16
  • Possible duplicate of [Generate unique random numbers between 1 and 100](http://stackoverflow.com/questions/2380019/generate-unique-random-numbers-between-1-and-100) – Peter O. Apr 29 '17 at 11:51

1 Answers1

5

Make an array of a 100 numbers an cut the chosen number out each time you call it:

var unique = (function() {                             // wrap everything in an IIFE
  var arr = [];                                        // the array that contains the possible values
  for(var i = 0; i < 100; i++)                         // fill it
    arr.push(i);
  
  return function() {                                  // return the function that returns random unique numbers
    if(!arr.length)                                    // if there is no more numbers in the array
      return alert("No more!");                        // alert and return undefined
    
    var rand = Math.floor(Math.random() * arr.length); // otherwise choose a random index from the array
    return arr.splice(rand, 1) [0];                    // cut out the number at that index and return it
  };
})();

console.log(unique());
console.log(unique());
console.log(unique());
console.log(unique());
ibrahim mahrir
  • 31,174
  • 5
  • 48
  • 73
  • This will work of course. It'd be interesting to do a performance comparison with a Fisher-Yates shuffle. I'm not sure which would be faster, given all the array optimizations in modern JavaScript runtimes. – Pointy Apr 28 '17 at 19:23
  • @Pointy for such a small array (100 elements), I don't think there will be a lot of difference in performance between the two methods. – ibrahim mahrir Apr 28 '17 at 19:24
  • @ibrahimmahrir Thanks for the response. Greatly appreciate it. But with the above way, I will have to click more than 100 times on my button to call the function and generate unique numbers. Is there any way to be able to call this function only 100 times and expect unique numbers? – Testaccount Apr 28 '17 at 19:35
  • @SrikanthBandaru I'm not sure I understand what you mean. Can you explain more? – ibrahim mahrir Apr 28 '17 at 19:37
  • @ibrahimmahrir I am sorry my bad. I thought something else was going on but looks like we are not getting truly unique numbers. https://s16.postimg.org/vrmq03zvp/Capture.jpg – Testaccount Apr 28 '17 at 19:45
  • @SrikanthBandaru You are probably redifining `unique`. `var unique = ...` should only be executed once. If you have a click event listener, then `unique` should be defined outside it. – ibrahim mahrir Apr 28 '17 at 19:46
  • @SrikanthBandaru Can you make a jsFiddle of how you're using it? – ibrahim mahrir Apr 28 '17 at 19:48
  • @ibrahimmahrir Here Sir! I have made one quickly. https://jsfiddle.net/x9e1e6vw/2/ – Testaccount Apr 28 '17 at 19:56
  • @SrikanthBandaru As I said: you're redifining it everytime you call `generateNumber`. The function `unique` should be only defined once (perhaps in the constructor or before the class). – ibrahim mahrir Apr 28 '17 at 20:03
  • @ibrahimmahrir Thanks for pointing that out! I have declared it outside the class and it is working just like how it should. :) – Testaccount Apr 28 '17 at 20:08