admin管理员组文章数量:1305167
I have a variable that has a number between 1-3.
I need to randomly generate a new number between 1-3 but it must not be the same as the last one.
It happens in a loop hundreds of times.
What is the most efficient way of doing this?
I have a variable that has a number between 1-3.
I need to randomly generate a new number between 1-3 but it must not be the same as the last one.
It happens in a loop hundreds of times.
What is the most efficient way of doing this?
Share Improve this question asked Apr 17, 2012 at 10:24 Moshe ShahamMoshe Shaham 16k24 gold badges81 silver badges122 bronze badges 7- 1 is it between 1-3, are we talking whole numbers? so options are 1, 2 or 3? – filype Commented Apr 17, 2012 at 10:26
- 2 You realise that generating a number from 1-3 without repeating is a) not random and b) functionally equivalent to selecting one of two numbers, i.e. probability 0.5? – Widor Commented Apr 17, 2012 at 10:26
- 1 ok i understand b) but why a)? – Moshe Shaham Commented Apr 17, 2012 at 10:28
- What have you tried so far? The efficiency of the loop depends on many things: Are you using this random in the loop only, should it be passed forward as an argument etc. – Teemu Commented Apr 17, 2012 at 10:39
- @Moshe because for it to be truly random, each number would have to be independent of the last. As you have the condition that there must be no repeats, the future numbers are affected by the past. – Widor Commented Apr 17, 2012 at 10:41
6 Answers
Reset to default 5May the powers of modular arithmetic help you!!
This function does what you want using the modulo operator:
/**
* generate(1) will produce 2 or 3 with probablity .5
* generate(2) will produce 1 or 3 with probablity .5
* ... you get the idea.
*/
function generate(nb) {
rnd = Math.round(Math.random())
return 1 + (nb + rnd) % 3
}
if you want to avoid a function call, you can inline the code.
Here is a jsFiddle that solves your problem : http://jsfiddle/AsMWG/
I've created an array containing 1,2,3 and first I select any number and swap it with the last element. Then I only pick elements from position 0 and 1, and swap them with last element.
var x = 1; // or 2 or 3
// this generates a new x out of [1,2,3] which is != x
x = (Math.floor(2*Math.random())+x) % 3 + 1;
You can randomly generate numbers with the random number generator built in to javascript. You need to use Math.random()
.
If you're push()
-ing into an array, you can always check if the previously inserted one is the same number, thus you regenerate the number. Here is an example:
var randomArr = [];
var count = 100;
var max = 3;
var min = 1;
while (randomArr.length < count) {
var r = Math.floor(Math.random() * (max - min) + min);
if (randomArr.length == 0) {
// start condition
randomArr.push(r);
} else if (randomArr[randomArr.length-1] !== r) {
// if the previous value is not the same
// then push that value into the array
randomArr.push(r);
}
}
As Widor mented generating such a number is equivalent to generating a number with probability 0.5. So you can try something like this (not tested):
var x; /* your starting number: 1,2 or 3 */
var y = Math.round(Math.random()); /* generates 0 or 1 */
var i = 0;
var res = i+1;
while (i < y) {
res = i+1;
i++;
if (i+1 == x) i++;
}
The code is tested and it does for what you are after.
var RandomNumber = {
lastSelected: 0,
generate: function() {
var random = Math.floor(Math.random()*3)+1;
if(random == this.lastSelected) {
generateNumber();
}
else {
this.lastSelected = random;
return random;
}
}
}
RandomNumber.generate();
本文标签: javascriptgenerate a new random numberStack Overflow
版权声明:本文标题:javascript - generate a new random number - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741790802a2397644.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论