2

can you please take a look at this demo and let me know how I can create 3 unique values from an array of numbers?

var num = [];
var chances = "0123456789";
for (var i = 0; i < 3; i++) {
  num.push(chances.charAt(Math.floor(Math.random() * chances.length)));
}

console.log(num);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Tushar
  • 85,780
  • 21
  • 159
  • 179
Behseini
  • 6,066
  • 23
  • 78
  • 125
  • 2
    Why not just `Math.floor((Math.random() * 10))` and as the values are _random_, that doesn't mean it should be **unique**, especially when creating a single-digit random value – Tushar Oct 06 '15 at 05:26
  • Hi Tushar I already tried it but was n't sure how o limit the number of selected item to 3?! – Behseini Oct 06 '15 at 05:31
  • Do you want three unique characters from your string with no repeats? What you have is not actually an array of characters - it's a string. – jfriend00 Oct 06 '15 at 05:31
  • Unique randoms here: [unique random array javascript](http://stackoverflow.com/questions/22399740/unique-random-array-javascript/22400087#22400087) and [Generating random numbers on different input texts and ensuring random numbers shown are unique](http://stackoverflow.com/questions/23401012/generating-random-numbers-on-different-input-texts-and-ensuring-random-numbers-s/23401087#23401087) and [Javascript generate random unique number every time](http://stackoverflow.com/questions/7045615/javascript-generate-random-unique-number-every-time/7046877#7046877). – jfriend00 Oct 06 '15 at 05:34

2 Answers2

2

You can do something like this

var num = [];
for (var i = 0; i < 3;) {
  var ran = Math.floor(Math.random() * 10);
  //  You can generate random number between 0-9 using this , suggested by @Tushar
  if (num.indexOf(ran) == -1)
  // check number is already in array
    num[i++] = ran;
    // if not then push the value and increment i
}

document.write(num.join());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

For getting unique alphabets

var res = [],
  alp = 'abcdefghijklmnopqrstuvwxyz'.split('');
  // creating an array of alphabets for picking alphabers
for (var i = 0; i < 3;) {
  var ran = Math.floor(Math.random() * 26);
  //  You can generate random number between 0-25 using this
  if (res.indexOf(alp[ran]) == -1)
  // check alphabet is already in array
    res[i++] = alp[ran];
  // if not then push the value and increment i
}

document.write(res.join());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Or

var res = [],
  alp = 'abcdefghijklmnopqrstuvwxyz'.split('');
  // creating an array of alphabets for picking alphabers
for (var i = 0; i < 3;) {
  var ran = Math.floor(Math.random() * 10000) % 26;
  //  You can generate random number between 0-25 using this
  if (res.indexOf(alp[ran]) == -1)
  // check alphabet is already in array
    res[i++] = alp[ran];
  // if not then push the value and increment i
}

document.write(res.join());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
0

You may also try this code:

var num = [];
var chances = "0123456789";
var len = chances.length;
var str;
while (num.length < 3) {
  str = "";
  while (str.length < len)
    str += chances[Math.floor((Math.random() * len) % len)];
  if (-1 === num.indexOf(str))
    num.push(str);
}
document.write(JSON.stringify(num));