// Random uniform integer generator // by Pat Lorch Version 1.0. // Generates random integers in a user defined range. // For example, if you set the range from 1 to 6, the output // will be like rolling a die. tap() { int x, y; // Wait for a pen up event waitp(); x = penx(); y = peny(); puts(x+","+y+"\n"); if (x>73 && x<103){ if( y>108 && y<123) return -1; if( y>33 && y<52) return 1; if( y>53 && y<72) return 2; } else return 3; } strtoi(string s) { int i,j=1,k,num=0; for(i=strlen(s)-1; i>-1;i--){ k=s@[i]-48; num=num+k*j; j=j*10; } return num; } getval(string lorh, string dval) { string val; val=getsd("Enter value for "+lorh, dval); if(val=="") return dval; else return val; } randu(int low, int high) { int i,n; float u; n=high-low+1; u=rand(); //rand btwn 0 & 1 for(i=1;i<=n;i++) if(u<=(float)i/(float)n) return(low+i-1); } main() { int low=0,high=10,x,y,test,num; string inval; clear(); puts("Random number generator\n\nGenerates random integers in user-specified range.\nTap anywhere to start...\n\n"); wait(); graph_on(); title("Random number generator"); text(23,35,"low"); text(23,55,"high"); text(23,75,"number"); text(79,111,"quit"); frame(1,73,33,103,52,0); frame(1,73,53,103,72,0); frame(1,73,73,103,92,0); frame(1,73,108,103,123,0); text(76,35,low); text(76,55,high); text(76,75,num); while(1) { test=tap(); puts(test+"\n"); if(test==-1) return; // quit switch(test) { case 1: inval=getval("Low",low); low=strtoi(inval); break; case 2: inval=getval("High",high); high=strtoi(inval); break; case 3: break; default: break; } if(low>=high){ alert("low >= high. Fix it!"); num= -999; } else num=randu(low,high); text(76,35," "); text(76,55," "); text(76,75," "); text(76,35,low); text(76,55,high); text(76,75,num); } }