p5.js | random()函数
p5.js 中的random()函数用于返回作为参数给出的范围之间的随机浮点数。
句法:
random(Min, Max)
或者
random(Array)
参数:此函数接受三个参数,如上所述,如下所述:
- Min:这是将要创建的随机数的下限。这是一个包含创建的随机数的数字。
- Max:这是将要创建的随机数的上限。这是创建的随机数的独占数。
- 数组:这是一个包含一些元素的数组,从中返回任何随机数。
返回值:返回随机数。
下面的程序说明了 p5.js 中的 random()函数:
示例 1:此示例使用 random()函数返回给定范围之间的随机浮点数。
function setup() {
// Creating Canvas size
createCanvas(550, 140);
// Set the background color
background(220);
// Calling to random() function with
// min and max parameters
let A = random(1, 2);
let B = random(0, 1);
let C = random(2);
let D = random(2, 10);
// Set the size of text
textSize(16);
// Set the text color
fill(color('red'));
// Getting random number
text("Random number between 1 and 2 is: " + A, 50, 30);
text("Random number between 0 and 1 is: " + B, 50, 60);
text("Random number between 0 and 2 is: " + C, 50, 90);
text("Random number between 2 and 10 is: " + D, 50, 110);
}
输出:
注意:在上面的代码中,在变量“C”中只传递了一个参数,然后它返回一个从下限0到该数字上限的随机数。
示例 2:此示例使用 random()函数返回给定范围之间的随机浮点数。
function setup() {
// Creating Canvas size
createCanvas(550, 140);
// Set the background color
background(220);
// Calling to random() function with
// parameter array of some elements
let A = random([1, 2, 3, 4]);
let B = random([0, 1]);
let C = random([2, 6, 7, 9]);
let D = random([2, 10]);
// Set the size of text
textSize(16);
// Set the text color
fill(color('red'));
// Getting random number
text("Random number is: " + A, 50, 30);
text("Random number is: " + B, 50, 60);
text("Random number is: " + C, 50, 90);
text("Random number is: " + D, 50, 110);
}
输出:
参考: https://p5js.org/reference/#/p5/random