![]() | (back to the list of tutorials) |
add_library('igeo') size( 480, 360, IG.GL ) num = 100 cpts1 = [] for i in range(num) : cpts1.append( IVec(i*2-50, 40, IRand.get()*10) ) ICurve(cpts1).clr(1.,0,0) cpts2 = [] for i in range(num) : cpts2.append([]) cpts2[i].append(IVec(i*2-50,-30, IRand.get()*5*(sin(i*0.2)+1) )) cpts2[i].append(IVec(i*2-50, 30, IRand.get()*5*(sin(i*0.2)+1) )) 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.
add_library('igeo') size( 480, 360, IG.GL ) for i in range(40) : ynum = IRand.getInt(10, 40) for j in range(ynum) : znum = IRand.getInt(20-j, 40-j) for k in range(znum) : size = IRand.get(2.5, 10.0) x=i*10 y=j*10 z=k*2 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)
add_library('igeo') size( 480, 360, IG.GL ) for i in range(1000) : pos = IRand.pt(-70,-70,-30,70,70,30) 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().
add_library('igeo') size( 480, 360, IG.GL ) IRand.initByTime() pts = [] for i in range(100) : pts.append( IRand.pt(-90,-90,-60,90,90,60) ) 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.
add_library('igeo') size( 480, 360, IG.GL ) for i in range(1000) : IPoint(IRand.pt(-70,-70,-30,0,70,30)).clr(IRand.clr()) IPoint(IRand.pt(0,-70,-30,70,70,30)).clr(IRand.gray())
Another example of random colors with surfaces.
add_library('igeo') size( 480, 360, IG.GL ) pt = IVec(30,30,0) for i in range(100) : ISurface(IRand.pt(-50,-50,0,0,0,50), \ IRand.pt(-50,-50,0,0,0,50), \ pt).clr(IRand.clr())
add_library('igeo') size( 480, 360, IG.GL ) for i in range(50) : for j in range(50) : x = i*4-100 y = j*4-100 # 50% probability to be executed if IRand.pct(50) : ISurface(x,y,0,x+2,y,0,x+2,y,50).clr(1.,1.,1.) # 50% of the rest ( = 25%) elif IRand.pct(50) : 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%) elif IRand.pct(50) : ISurface(x,y,0,x+2,y,0,x+2,y,10).clr(1.,0,0)