Randomize value in Javascript

2019-04-14 16:18发布

I need to randomize set of values in JS, and I call function randomize for example three times. How can I remember and block random generator from giving me results that it gave me previous times? I can only give one value once.

var value = Math.floor(Math.random() * 11);

8条回答
Root(大扎)
2楼-- · 2019-04-14 16:29

One of the wonderful randoms are provided by allocating sequential array of int:

for(i = 0; i < 100; ++i) myArr[i]=i;

And after it you shift numbers there in order Math.floor(Math.random() * myArr.length);

benefit of such approach - you get non-repeatable sequence of any length.

查看更多
该账号已被封号
3楼-- · 2019-04-14 16:31

Judging from your comments, it sounds like you have a small range of values (i.e. from 1-10) which you want to randomly select your values from. In that case your best bet is to store your values in an array and randomly splice them out. I prefer to perform these kinds of operations using some sort of generator.

function createRandomGenerator( array ) {
    return function() {
        return array.splice(Math.floor(Math.random()  * array.length ),1)[0];
    }
}

To use the generator creator, provide it with your short list of unique values, and store the result. The result is a generator you can use to generate N number of random numbers from your list of unique values. (Where N is the length of the array you seeded your random generator with).

var random1to10 = createRandomGenerator( [1,2,3,4,5,6,7,8,9,10] );

You can then use the generator by calling the generator that that function returns over and over.

var a = [];
for( var i = 0; i < 10; i++ ) {
    a.push( random1to10() );
}
alert(a.join(','));

If you do not have a pre-defined set of numbers you wish to use, I'd recommend Török Gábor's solution above.

Cheers!

查看更多
我想做一个坏孩纸
4楼-- · 2019-04-14 16:35

Instead of using Random(), you can use time ticks which will give you a random value everytime you use them -

var random = (new Date()).getTime();


Or, you can extend the default JS Array, and add the contains method to it. Every random value you generate will be added to the array, but before adding, the contains method will check whether it already exists in the array or not.

查看更多
别忘想泡老子
5楼-- · 2019-04-14 16:38

Use a hash.

var values = {};
count = 0; //keep count of how many are there if you want.
while( count < something ) {
    var rand = Math.random();
    if( !values[rand] ) {
        values[rand] = true;
        count++;
    }
}

Then you can dump all of those into an array if you want - that's O(n).

var arrValues = [];
for( var p in values ) arrValues.push( new Number(p) );
查看更多
别忘想泡老子
6楼-- · 2019-04-14 16:44

Something like this (tested):

Array.prototype.unique = function () {
    var r = new Array();
    o:for(var i = 0, n = this.length; i < n; i++)
    {
        for(var x = 0, y = r.length; x < y; x++)
        {
            if(r[x]==this[i])
            {
                continue o;
            }
        }
        r[r.length] = this[i];
    }
    return r;
}

var temp = [];

//keep going until the array size is three
while(temp.length < 3) {
    temp.push(Math.floor(Math.random() * 11));
    //unique() will remove the dupes thus affecting the length 
    temp = temp.unique();
}

alert(temp[0] + ' ' + temp[1] + ' ' + temp[2]);
查看更多
可以哭但决不认输i
7楼-- · 2019-04-14 16:45

What is your larger goal? If you're trying to select items from a finite set in a random order, then you'll want to look at using a shuffle algorithm to randomize the ordering and then remove them in the resulting shuffled order as needed.

查看更多
登录 后发表回答