ランダムの数を発生させる基本は random(数)を使う方法です。以下の例では0〜100までの範囲から選ばれたランダムの数が表示されます。
1 2 3 4 5 |
println(random(100)); println(random(100)); println(random(100)); println(random(100)); println(random(100)); |
randomSeed(数)を使うと、乱数のテーブルが固定されるので、毎回同じ乱数が出ます。以下の例では、毎回同じ組み合わせの数字が出ます。randomSeed(数字)の数字が同じである場合には、毎回同じ組み合わせになります。
1 2 3 4 5 6 7 |
randomSeed(5); println(random(100)); println(random(100)); println(random(100)); println(random(100)); println(random(100)); |
ちなみに99〜100までの乱数を出す場合にはrandom(数1〜数2)で指定します。
1 2 3 4 5 |
println(random(99,100)); println(random(99,100)); println(random(99,100)); println(random(99,100)); println(random(99,100)); |
以下引用
randomSeed()で乱数表を固定することで、最初のラインを引くフェーズも、次の点を打つフェーズも、同じ乱数がでる。そのため、ランダムであるにもかからわず、ラインと点で同じ乱数を使うことができる。
マウスを離したときにrandomSeed()の値を変えている。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
/** * draws a random chart and shows how to use randomSeed. * * MOUSE * click : new random line * * KEYS * p : save pdf * s : save png */ import processing.pdf.*; import java.util.Calendar; boolean savePDF = false; int actRandomSeed = 42; void setup() { size(1024, 256); smooth(); } void draw() { if (savePDF) beginRecord(PDF, timestamp()+".pdf"); background(255); // line stroke(0,130,164); strokeWeight(1); strokeJoin(ROUND); noFill(); randomSeed(actRandomSeed);//ランダムシードの数をまず入れる。最初は4を入れる beginShape(); for (int x = 0; x < width; x+=10) { float y = random(0,height); //乱数を10回作ることになる。 vertex(x,y); } endShape(); // dots noStroke(); fill(0); randomSeed(actRandomSeed);//ここで再度ランダムシードを4に設定することで乱数表がリセットされる for (int x = 0; x < width; x+=10) { float y = random(0,height); //1個前の乱数をつくったときと、同じ乱数が発生する ellipse(x,y,3,3); } if (savePDF) { savePDF = false; endRecord(); } } void mouseReleased() { actRandomSeed = (int) random(100000);//マウスを離すたびに、randomSeedの値を変える } void keyReleased() { if (key == 's' || key == 'S') saveFrame(timestamp()+"_####.png"); if (key == 'p' || key == 'P') savePDF = true; } String timestamp() { Calendar now = Calendar.getInstance(); return String.format("%1$ty%1$tm%1$td_%1$tH%1$tM%1$tS", now); } // M_1_1_01.pde // // Generative Gestaltung, ISBN: 978-3-87439-759-9 // First Edition, Hermann Schmidt, Mainz, 2009 // Hartmut Bohnacker, Benedikt Gross, Julia Laub, Claudius Lazzeroni // Copyright 2009 Hartmut Bohnacker, Benedikt Gross, Julia Laub, Claudius Lazzeroni // // http://www.generative-gestaltung.de // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. |
Noise
Perlin noise とよばれる乱数を生成します。この乱数の特徴は、変化が少ないところです。次のコードを実行するとわかりますが、隣と隣が比較的近い値が出ています。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
void setup(){ size(1000,300); noFill(); } void draw(){ noiseSeed(2); beginShape(); for (int x = 0; x < width; x+=10) { float noiseX = map(x, 0,width, 0,10); float y = noise(noiseX) * height; vertex(x,y); } endShape(); } |
こちらも、noiseSeedで乱数を固定することができます。noiseSeedが同一で、かつnoise(X)のXの値が同じであれば、同じ値が出ます。
1 2 3 4 5 |
noiseSeed(1); println(noise(0)); println(noise(1)); println(noise(2)); println(noise(3)); |
One comment