1

I am using the following code which generates random number between 0 to Totalfriends, I would like to get the random numbers but they should not be repeated. Any idea how?

This is the code I am using

FB.getLoginStatus(function(response) {
    var profilePicsDiv = document.getElementById('profile_pics');
FB.api({ method: 'friends.get' }, function(result) {

     // var result =resultF.data;
   // console.log(result);
   var user_ids="" ;
   var totalFriends = result.length;
   // console.log(totalFriends);
   var numFriends = result ? Math.min(25, result.length) : 0;
  // console.log(numFriends);
   if (numFriends > 0) {
      for (var i=0; i<numFriends; i++) {
        var randNo = Math.floor(Math.random() * (totalFriends + 1))
        user_ids+= (',' + result[randNo]);
         console.log(user_ids);

          }
        }
        profilePicsDiv.innerHTML = user_ids;
      });
});
1
  • You should be using Math.floor(Math.random() * totalFriends), else you'll sometimes go past the end of the array Commented May 25, 2012 at 14:26

4 Answers 4

3

Here's a function that will take n random elements from array, and return them, based off a fisher-yates shuffle. Note that it will modify the array argument.

function randomFrom(array, n) {
    var at = 0;
    var tmp, current, top = array.length;

    if(top) while(--top && at++ < n) {
        current = Math.floor(Math.random() * (top - 1));
        tmp = array[current];
        array[current] = array[top];
        array[top] = tmp;
    }

    return array.slice(-n);
}

Assuming your code works how I think it does, you already have an array of userids:

var random10 = randomFrom(friendIds, 10);
Sign up to request clarification or add additional context in comments.

2 Comments

I have Update the complete code , I am actually getting a list of ID's from Those list of ID's i am select random 25 .
So should i add this randomFrom function in the page and then call it inplace of randNo ?
2
  1. create an array (e.g. yourarray) of numbers in range [1..totalfriends]
  2. shuffle the array (e.g. using a javascript implementation of Fisher-Yates algorithm)
  3. inside the for (from 0 to yourarray.length - 1) make a pop() from the array (or just get the n-th element) so you will get everytime a different number

Doing so you you will avoid to get duplicated numbers

2 Comments

I am unfimilar with the pop() Function, How could i implement it here. ?
pop is already implemented in javascript. it returns the last element from the array
0

I would perform random iterations, create an array with all your numbers in, such as:

var friendIndexes = [];

for (var i=0; i<numFriends; i++)
{
   friendIndexes.push(i);
}

Then once you have an array of all the numbers, I would perform some number of iterations, maybe 1,000, where you generate two random numbers, and swap the values in those indexes.

for (var s = 0; s<1000; s++)
{
    var rnd1 = Math.floor(Math.random() * (numFriends + 1);
    var rnd2 = Math.floor(Math.random() * (numFriends + 1);

    // Swap the two values (remember to use a temp variable)
    var tmp = friendIndexes[rnd1];
    friendIndexes[rnd1] = friendIndexes[rnd2];
    friendIndexes[rnd2] = tmp;
}

You're essentially shuffling them, and the result is going to give you the numbers in a random order.

2 Comments

The idea is good, but the Fisher-Yates shuffle is a much more efficient way to shuffle an array.
I have Update the complete code , I am actually getting a list of ID's from Those list of ID's i am select random 25
0

Take a big number wich not divide numFriends or just a big prime number (like one : 702038, 727699, 992700, 1201046, 1232255, 2312734, 3136255, 4235414, 6090515) then goes

var result=[] ;
var K=Math.floor((Math.random()*bigUnNumFreindsDivider) ;

for (var i=0; i<numFriends; i++)
{
    result[i]=(i*bigUnNumFreindsDivider+K)%numFreinds ;
}

This should work fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.