0

I want to send the App req to 5 people , i have managed to get the UserID's of 5 people but its always those same 5 people , isnt there a way to randomise the USERID's which i get from Facebook ?

<script>
var profilePicsDiv = document.getElementById('profile_pics');

FB.getLoginStatus(function(response) {


  FB.api({ method: 'friends.get' }, function(result) {
    Log.info('friends.get response', result);
   var user_ids="" ;
    var numFriends = result ? Math.min(5, result.length) : 0;
    if (numFriends > 0) {
      for (var i=0; i<numFriends; i++) {
        user_ids+= (

                          ',' + result[i] 

        );
      }
    }
    profilePicsDiv.innerHTML = user_ids;
alert(user_ids);

  });
});


 function sendRequestToRecipients() {
        var user_ids = document.getElementsByName("user_ids")[0].value;
        FB.ui({method: 'apprequests',
          message: 'My Great Request',
          to: user_ids,                        ///  How to Fill the ID's HERE ?
        }, requestCallback);
      }


</script>
Yahoo
  • 4,093
  • 17
  • 59
  • 85

2 Answers2

1

I havent tested, but this should work,

FB.getLoginStatus(function(response) {
 FB.api({ method: 'friends.get' }, function(result) {
   Log.info('friends.get response', result);
   var user_ids="" ;
   var totalFriends = result.length;
   var numFriends = result ? Math.min(5, result.length) : 0;
   if (numFriends > 0) {
      for (var i=0; i<numFriends; i++) {
        var randNo = Math.floor(Math.random() * totalFriends)
        user_ids+= (',' + result[randNo]);
      }
    }
    profilePicsDiv.innerHTML = user_ids;
alert(user_ids);

  });
});

Here, in loop I generate a random no from 0 to result.length (i.e. total friends in current response) And then I use that random no to fetch random id from the given list.

Edit: (after OP asked about non-repeating randoms),

Based on your requirement, this should work...

   FB.getLoginStatus(function(response) {
     FB.api({ method: 'friends.get' }, function(result) {
       Log.info('friends.get response', result);
       var user_ids="" ;
       var totalFriends = result.length;
       var randNo = Math.floor(Math.random() * totalFriends);
       var numFriends = result ? Math.min(5,totalFriends) : 0;
       if (numFriends > 0) {
          for (var i=0; i<numFriends; i++) { 
            user_ids+= (',' + result[randNo]);
            randNo ++;
            if(randNo >= totalFriends){
                randNo = 0;
            } 
          }
        }
        profilePicsDiv.innerHTML = user_ids;
    alert(user_ids);

      });
    });

Here, instead of generating random no each time, I generate a random no once and then increment it. If random no exceeds the total no of friends, then I start from 0.

This will always give random friends each time :)

Code is not tested, I apologize if has errors ( But code surely gives you a direction to think)

Jashwant
  • 28,410
  • 16
  • 70
  • 105
  • Thank You Buddy , but the random number that are generated are repeated twice some time , is there a way to avoid the duplication of the random numbers ? – Yahoo May 25 '12 at 14:11
  • `This would give me a random number X then x+1 , x+2 ,x+3 .. so on ?? :-)` – Yahoo May 25 '12 at 19:46
  • 1
    yes, If you have 100 friends, sometime it will send request to 88,89,90,91,92th friends , at another time, it will send request to 99,100,1,2,3rd friend and so on. A different set each time and no UID repeats in the set. – Jashwant May 25 '12 at 19:48
  • Well , it will serve my purpose , Thanks :) – Yahoo May 25 '12 at 19:49
0

You are now always taking friends 0, 1, 2, 3, and 4 from the result. If the result is longer then you'll never get those friend ids. You should calculate 5 random integers between 0 and length - 1 (inclusive) and get those instead (for example, result[3], result[84], et cetera). You should then send invites to those friends.

To compute a random number between 0 and result.length - 1, see this StackOverflow question: Generating random whole numbers in JavaScript in a specific range? .

I'm not that familiar with the FB.ui function but you should check whether you are giving the numbers in the way it expects them. Does it want them comma-separated as you do now? Or do you have to call it five times, once for each app request? That's something you should look up. If it's the latter then you have to iterate over the user_ids and call FB.ui with each user id.

Community
  • 1
  • 1
Simeon Visser
  • 118,920
  • 18
  • 185
  • 180
  • How do i setup this to get random ID's ? This code is also not working with the 5 ID's , Dont know why ? – Yahoo May 24 '12 at 20:37
  • I have updated my answer with more info on computing a random number and possibly why your code is not working. – Simeon Visser May 24 '12 at 20:43