// $Id: test_random.exper,v 1.8 2002/04/15 08:45:57 boissieu Exp $ define float loops = 1000 for loops do // random() gives a float on [0..1] define float x = random() if x<0.0 || x>=1.0 then error "random(E) range error" end end for loops do // random(n) gives a float on [0..n] set x = random(3) if x<0.0 || x>=3.0 then error "random(E) range error : " x end end for loops do // random(n,p) gives a float on [n..p] set x = random(3,5) if x<3.0 || x>=5.0 then error "random(E,E) range error : " x end end /* * Build an histogram of the random generator. */ define float a = 0 define float b = 0 define float c = 0 define float d = 0 define float e = 0 define float f = 0 define float g = 0 define float h = 0 define float i = 0 define float j = 0 for 10*loops do choose random() in set a = a+1 set b = b+1 set c = c+1 set d = d+1 set e = e+1 set f = f+1 set g = g+1 set h = h+1 set i = i+1 set j = j+1 end end /* * Check the histogram */ define float k = 0 // The value to check. bloc check is set k = |k - loops| / loops // Relative error. if k>0.1 then error "random() not evenly distributed" end end set k = loops check set k = b+0 check set k = a check set k = c check set k = d check set k = e check set k = f check set k = g check set k = h check set k = i check set k = j check print "========= test_random passed OK" quit