1. <tfoot id='nRz3H'></tfoot>

    <small id='nRz3H'></small><noframes id='nRz3H'>

    1. <legend id='nRz3H'><style id='nRz3H'><dir id='nRz3H'><q id='nRz3H'></q></dir></style></legend>
    2. <i id='nRz3H'><tr id='nRz3H'><dt id='nRz3H'><q id='nRz3H'><span id='nRz3H'><b id='nRz3H'><form id='nRz3H'><ins id='nRz3H'></ins><ul id='nRz3H'></ul><sub id='nRz3H'></sub></form><legend id='nRz3H'></legend><bdo id='nRz3H'><pre id='nRz3H'><center id='nRz3H'></center></pre></bdo></b><th id='nRz3H'></th></span></q></dt></tr></i><div id='nRz3H'><tfoot id='nRz3H'></tfoot><dl id='nRz3H'><fieldset id='nRz3H'></fieldset></dl></div>
        <bdo id='nRz3H'></bdo><ul id='nRz3H'></ul>
    3. 如何生成不重复的随机数 javascript

      时间:2023-06-13
      <i id='IzlNx'><tr id='IzlNx'><dt id='IzlNx'><q id='IzlNx'><span id='IzlNx'><b id='IzlNx'><form id='IzlNx'><ins id='IzlNx'></ins><ul id='IzlNx'></ul><sub id='IzlNx'></sub></form><legend id='IzlNx'></legend><bdo id='IzlNx'><pre id='IzlNx'><center id='IzlNx'></center></pre></bdo></b><th id='IzlNx'></th></span></q></dt></tr></i><div id='IzlNx'><tfoot id='IzlNx'></tfoot><dl id='IzlNx'><fieldset id='IzlNx'></fieldset></dl></div>

        <tbody id='IzlNx'></tbody>

      <legend id='IzlNx'><style id='IzlNx'><dir id='IzlNx'><q id='IzlNx'></q></dir></style></legend>

        <small id='IzlNx'></small><noframes id='IzlNx'>

          1. <tfoot id='IzlNx'></tfoot>
            • <bdo id='IzlNx'></bdo><ul id='IzlNx'></ul>
              • 本文介绍了如何生成不重复的随机数 javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在使用以下代码生成 0 到 Totalfriends 之间的随机数,我想获取随机数,但它们不应重复.知道怎么做吗?

                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?

                这是我正在使用的代码

                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;
                      });
                });
                

                推荐答案

                这是一个函数,它将从 array 中获取 n 个随机元素,并根据 Fisher-yates shuffle 返回它们.请注意,它将修改 array 参数.

                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);
                }
                

                假设您的代码按照我的想法运行,那么您已经拥有一组用户 ID:

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

                var random10 = randomFrom(friendIds, 10);
                

                这篇关于如何生成不重复的随机数 javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:正则表达式比较两个数字 下一篇:防止 input type=“number" 的值超过一个点并限制小数位数

                相关文章

                <legend id='Trf3a'><style id='Trf3a'><dir id='Trf3a'><q id='Trf3a'></q></dir></style></legend>

                    <bdo id='Trf3a'></bdo><ul id='Trf3a'></ul>

                  <tfoot id='Trf3a'></tfoot>
                  1. <small id='Trf3a'></small><noframes id='Trf3a'>

                    <i id='Trf3a'><tr id='Trf3a'><dt id='Trf3a'><q id='Trf3a'><span id='Trf3a'><b id='Trf3a'><form id='Trf3a'><ins id='Trf3a'></ins><ul id='Trf3a'></ul><sub id='Trf3a'></sub></form><legend id='Trf3a'></legend><bdo id='Trf3a'><pre id='Trf3a'><center id='Trf3a'></center></pre></bdo></b><th id='Trf3a'></th></span></q></dt></tr></i><div id='Trf3a'><tfoot id='Trf3a'></tfoot><dl id='Trf3a'><fieldset id='Trf3a'></fieldset></dl></div>