English 中文(简体)
如何生成随机数字, 不重复 Javacramp 。
原标题:How to generate random numbers with no repeat javascript

我使用以下代码来生成0到完全朋友之间的随机数字,我想得到随机数字,但不应重复。知道如何使用吗?

这是我使用的代码

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;
      });
});
最佳回答

s 这里有一个函数, 它会从 < code> array 中取出 n 随机元素, 并返回这些元素, 以 fisher- yates shuffle 为基础 。 注意它将修改 < code> array 参数 。

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

假设你的代码和我想的一样有效, 你已经有一系列的用词了:

var random10 = randomFrom(friendIds, 10);
问题回答
  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

这样你就能避免重复数字

我会执行随机迭代, 创建一个包含您所有数字的数组, 例如 :

var friendIndexes = [];

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

一旦你掌握了所有数字的阵列, 我会进行一些迭代, 可能1000次, 在那里你产生两个随机数字, 然后交换这些指数中的数值。

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

你基本上重新打乱了它们, 结果会按随机顺序给您数字。

使用一个大数字( wich) 不分割 numfriends 或只是一个大质数( 类似: 702038, 727699, 99270, 1201046, 1232255, 2312734, 3136255, 4235414, 6090515)

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

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

这应该行得通





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签