每次運行程序時,p5.js中的randomSeed()函數用於返回一個隨機數。 random()函數與randomSeed()函數之間的區別在於,每次運行程序時random()函數都會產生不同的值,但是當使用randomSeed()函數時,則每次運行程序時它將給出一個恒定的隨機數。
用法:
randomSeed( Seed )
參數:該函數接受單個參數Seed,該參數可以是任何整數值。
返回值:它返回一個恒定的隨機數。
以下程序說明了p5.js中的randomSeed()函數:
示例1:本示例使用randomSeed()函數每次運行程序時都返回一個隨機數。
function setup() {
// Creating Canvas size
createCanvas(550, 140);
// Set the background color
background(220);
// Calling to randomSeed() function
randomSeed(9)
// 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:本示例使用randomSeed()函數每次運行程序時都返回一個隨機數。
function setup() {
// Creating Canvas size
createCanvas(550, 140);
// Set the background color
background(220);
// Calling to randomSeed() function
randomSeed(9)
// 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/randomSeed
相關用法
- p5.js sin()用法及代碼示例
- p5.js tan()用法及代碼示例
- d3.js d3.hsl()用法及代碼示例
- p5.js cos()用法及代碼示例
- PHP pos()用法及代碼示例
- PHP key()用法及代碼示例
- p5.js log()用法及代碼示例
- d3.js d3.map.set()用法及代碼示例
- p5.js day()用法及代碼示例
- PHP each()用法及代碼示例
- p5.js second()用法及代碼示例
- PHP Ds\Map put()用法及代碼示例
注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 p5.js | randomSeed() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。