Rand(lo,hi) (Int)
Monkey Forums/Monkey Code/Rand(lo,hi) (Int)
| ||
Rand(359) would return 0 to 359 integer. Rand(5,10) would return well... Function Rand:Int(vmin:Int, vmax:Int=0) Local rnum:Int If Not vmax rnum = Rnd(0,vmin+.99) Else rnum = Rnd(vmin,vmax+.99) Endif Return rnum End Function |
| ||
Is there any benefit to this over using Rnd#( low#, high# ) wrapped in a rounding function? |
| ||
You should really do the one-parameter case by function overloading, because what if I want Rand(-1,0)? matt - this really is just Rnd wrapped in a rounding function. vmax+.99 as an upper bound doesn't *really* get you what you want, which is everything in the interval [vmin,vmax+1), ie vmin <= rnum < vmax+1. But in fact Monkey's Rnd function excludes the upper bound, so you can just do Int(Rnd(vmin,vmax+1)). |
| ||
Ah, thanks Warpy. That's what I was wondering. I prefer a simple Rand function for when I just want a random integer. Much nicer: Function Rand:Int(vmin:Int, vmax:Int) Return Rnd(vmin, vmax+1) End Function |
| ||
Sorry @Warpy, I meant the same as your conclusion - Rnd wrapped in a built-in rounding function. Int is even tidier than using Floor. |