简体   繁体   English

如何以每个随机数具有不同值的方式在多个位置调用随机数?

[英]How can I call a random number in multiply places in a way that each random number has a different value?

So I want to change 4 "a" element background-color and I want to use rgb colors in a way that each color has a random generated number,so the colors will be different every time I call with a function.The problem is that the generated number not changing but use the same random number on all element so it's colors the same.所以我想更改 4 个“a”元素背景颜色,并且我想以每种颜色都有一个随机生成的数字的方式使用 rgb 颜色,因此每次调用函数时颜色都会不同。问题是生成的数字不会改变,但在所有元素上使用相同的随机数,因此颜色相同。

I could fix that by putting random numbers in an Array and call random numbers from there but it's kind of complicated.我可以通过将随机数放入数组并从那里调用随机数来解决这个问题,但这有点复杂。

To sum it up:把它们加起来:

-I'm looking for a more compact solution to do that - 我正在寻找一个更紧凑的解决方案来做到这一点

-I also want to use some other method to call this function individually on every element...not in this querySelectorAll way... - 我还想使用其他一些方法在每个元素上单独调用这个函数......不是在这个 querySelectorAll 方式......

Hope everything was clear and Thanks for the answers!:)希望一切都清楚,感谢您的回答!:)

function changeCol() {
    var colors = document.querySelectorAll(".con a");
    var x =[255,180,45,99,191,11]
    var y =[25,210,67,49,101,22]
    var z =[156,194,55,9,88,33]

    if(x==x2 || y == y2 || z ==z2){

        return;
    } else {

     colors[0].style.backgroundColor ="rgba("+x[Math.ceil(Math.random()*5)]+","+x[Math.ceil(Math.random()*5)]+","+x[Math.ceil(Math.random()*5)]+")";

     colors[1].style.backgroundColor ="rgba("+x[Math.ceil(Math.random()*5)]+","+x[Math.ceil(Math.random()*5)]+","+x[Math.ceil(Math.random()*5)]+")";

     colors[2].style.backgroundColor ="rgba("+x[Math.ceil(Math.random()*5)]+","+x[Math.ceil(Math.random()*5)]+","+x[Math.ceil(Math.random()*5)]+")";

     colors[3].style.backgroundColor ="rgba("+x[Math.ceil(Math.random()*5)]+","+x[Math.ceil(Math.random()*5)]+","+x[Math.ceil(Math.random()*5)]+")";
    }

   x=x2;
   y=y2;
   z=z2;
}

JS Math.random() return a random number between 0 and 1. So you are getting a random number between 0 and 1, after doing ceil() always it is 1. Thats why you are getting always same number. JS Math.random()返回一个介于 0 和 1 之间的随机数。所以你得到一个介于 0 和 1 之间的随机数,在执行ceil()之后它总是 1。这就是为什么你总是得到相同的数字。

But you can get random number by setting a range like但是你可以通过设置一个范围来获得随机数

 function getRandomInt(max) { return Math.floor(Math.random() * Math.floor(max)); } console.log(getRandomInt(6))

MDN reference . MDN 参考

The function will generate a new color whenever called该函数将在每次调用时生成一种新颜色

function getRandomColor() {
  var letters = '0123456789ABCDEF';
  var color = '#';
  for (var i = 0; i < 6; i++) {
    color += letters[Math.floor(Math.random() * 16)];
  }
  return color;
}

And code will be a lot simpler而且代码会简单很多

var colors = document.querySelectorAll(".con a");
colors.forEach(element => {
    element.style.backgroundColor = getRandomColor()
})

if you need random color(s) you can use randomcolor .如果您需要随机颜色,您可以使用randomcolor
we have used in several projects without problems.我们已经在几个项目中使用了没有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM