random()

本参考资料用于Processing 3.0+。 如果你使用的是前一版本,请在使用软件帮助菜单内的参考资料。. If you see any errors or have suggestions, please let us know. If you prefer a more technical reference, visit the Processing Core Javadoc and Libraries Javadoc.

名称

random()

示例

for (int i = 0;

i <100;

i++)
{  
float r = random(50);


stroke(r*5);


line(50, i, 50+r, i);


  }

for (int i = 0;

i <100;

i++)
{  
float r = random(-50, 50);


println(r);


  }

 // Get a random element from an array String[] words ={ "apple", "bear", "cat", "dog"
  };

int index = int(random(words.length));

// Same as int(random(4))
println(words[index]);

// Prints one of the four words
描述 Generates random numbers. Each time the random() function is called, it returns an unexpected value within the specified range. If only one parameter is passed to the function, it will
return afloat between zero and the value of the high parameter. For example, random(5) returns values between 0 and 5 (starting at zero, and up to, but not including, 5).
If two parameters are specified, the function will
return afloat with a value between the two values. For example, random(-5, 10.2) returns values starting at -5 and up to (but not including) 10.2. To convert afloating-point random number to an integer, use the int() function.
语法
random(high) random(low, high)
参数
low float: lower limit
high float: upper limit
返回值 float
相关 randomSeed()
noise()

Updated on October 2, 2018 05:02:06pm EDT

random()