Tutorials (back to the list of tutorials)

## Randomness

### Random numbers

iGeo has a class IRandom (or its alias class IRand) to provide various random number methods. The most basic one is IRandom.get(). This returns a random number in double whose range is from 0.0 to 1.0.

```import processing.opengl.*;
import igeo.*;

size( 480, 360, IG.GL );

int num = 100;

IVec[] cpts1 = new IVec[num];
for(int i=0; i < num; i++){
cpts1[i] = new IVec(i*2-50, 40, IRandom.get()*10);
}
new ICurve(cpts1).clr(1.,0,0);

IVec[][] cpts2 = new IVec[num][2];
for(int i=0; i < num; i++){
cpts2[i][0] =
new IVec(i*2-50,-30, IRandom.get()*5*(sin(i*0.2)+1) );
cpts2[i][1] =
new IVec(i*2-50, 30, IRandom.get()*5*(sin(i*0.2)+1) );
}
new ISurface(cpts2);
```

There are also a method to generate random numbers within the specified range. To generate random integers, please use IRandom.getInt(int,int) with minimum and maximum integers specified in the arguments. To generate random double within a range, please use IRandom.get(double,double) with minimum and maximum double specified in the arguments.

```import processing.opengl.*;
import igeo.*;

size( 480, 360, IG.GL );

for(int i=0; i < 40; i++){

int ynum = IRandom.getInt(10, 40);
for(int j=0; j < ynum; j++){

int znum = IRandom.getInt(20-j, 40-j);
for(int k=0; k < znum; k++){

double size = IRandom.get(2.5, 10.0);
double x=i*10, y=j*10, z=k*2;
new ISurface(x, y, z, x+size, y, z,
x+size, y+size, z, x, y+size, z)
.clr(k*0.05, k*0.05, 0);
}
}
}
```

### Random Points

You can generate IVec with random location within the range between minimum x, y, z and maximum x, y, z, with IRandom.pt() method. The input arguments are 6 double of minimum x, y, z, and maximum x, y, z.

```import processing.opengl.*;
import igeo.*;

size( 480, 360, IG.GL );

for(int i=0; i < 1000; i++ ){
IVec pos = IRandom.pt(-70,-70,-30,70,70,30);
new IPoint(pos);
}
```

If you want to set a seed for random number generator, use IRandom.init(long) specifying the seed number as long integer in the argument. If you want to randomize the behavior of random number generator every time you execute your code automatically, use IRandom.initByTime().

```import processing.opengl.*;
import igeo.*;

size( 480, 360, IG.GL );

IRandom.initByTime();
IVec[] pts = new IVec[100];
for(int i=0; i < pts.length; i++ ){
pts[i] = IRandom.pt(-90,-90,-60,90,90,60);
}
new ICurve(pts,3).clr(1.,.3,0);
```

With IRandom.initByTime() you can change the behavior of random number every time you run your code. If you want to fix the behavior of random numbers, you can either do nothing to initialize (in this case, it's automatically initialized with default seed number), or set your own seed number with IRandom.init(long). With this method, you can change the behavior by change the seed number but if you want to replay the exactly same behavior later, you can do it by putting the same seed number.

### Random Colors

IRandom class has a method IRandom.clr() to generate a random color, and another method IRandom.gray() to generate a random gray scale color.

```import processing.opengl.*;
import igeo.*;

size( 480, 360, IG.GL );

for(int i=0; i < 1000; i++ ){
new IPoint(IRandom.pt(-70,-70,-30,0,70,30))
.clr(IRandom.clr());
new IPoint(IRandom.pt(0,-70,-30,70,70,30))
.clr(IRandom.gray());
}
```

Another example of random colors with surfaces.

```import processing.opengl.*;
import igeo.*;

size( 480, 360, IG.GL );

IVec pt = new IVec(30,30,0);

for(int i=0; i < 100; i++){
new ISurface(IRandom.pt(-50,-50,0,0,0,50),
IRandom.pt(-50,-50,0,0,0,50),pt)
.clr(IRandom.clr());
}
```

### Probabilistic Switch

You can use IRandom.percent() to create a condition which is switched probabilistically by putting percentage in its argument.

```import processing.opengl.*;
import igeo.*;

size( 480, 360, IG.GL );

for(int i=0; i < 50; i++){
for(int j=0; j < 50; j++){
double x = i*4-100, y = j*4-100;
// 50% probability to be executed
if(IRandom.percent(50)){
new ISurface(x,y,0,x+2,y,0,x+2,y,50).clr(1.,1.,1.);
}
// 50% of the rest ( = 25%)
else if(IRandom.percent(50)){
new ISurface(x,y,0,x+2,y,0,x+2,y,20).clr(0,1.,1.);
}
// 50% of the rest of the rest ( = 12.5%)
else if(IRandom.percent(50)){
new ISurface(x,y,0,x+2,y,0,x+2,y,10).clr(1.,0,0);
}
}
}
```

(back to the list of tutorials)