Jack Kelly
New Member
I see no benefit from anonymity.
Posts: 19
|
Post by Jack Kelly on May 26, 2024 16:12:34 GMT -5
There's a short, elegant routine for returning a random number between n and m. Does anyone happen to have it?
|
|
|
Post by tsh73 on May 26, 2024 17:09:04 GMT -5
Hello Jack Here what I have in my _snippets folder.
'***************************************************** 'function returns a random integer in range [Min, Max) function randRange(Min, Max) randRange = Min + random(Max-Min) end function
'***************************************************** 'function returns a random integer in range [0, n) function random(n) random = int(rnd(1)*n) end function
|
|
|
Post by tenochtitlanuk on May 26, 2024 17:18:16 GMT -5
Fun to watch it throwing lots of times... nomainwin
WindowWidth =400 WindowHeight =470
dim numbers( 40)
open "Rand demo" for graphics_nsb as #wg
#wg "trapclose quit"
#wg "down ; size 9 ; color darkblue"
for k =0 to 40 #wg "set "; 10 +k *10; " 430 " next k
#wg "color red ; size 6"
for i =1 to 60000 X =IntRndInRange( 3, 20) numbers( X) =numbers( X) +1 #wg "line "; 10 +X *10; " 430 "; 10 +X *10; " "; 430 -numbers( X) /10 scan next i
#wg "flush"
wait
function IntRndInRange( a, b) IntRndInRange =a +int( rnd( 1) *( 1 +b -a)) end function
sub quit h$ close #h$ end end sub
|
|
Jack Kelly
New Member
I see no benefit from anonymity.
Posts: 19
|
Post by Jack Kelly on May 26, 2024 18:03:19 GMT -5
Thanks guys. Love you both...
|
|
|
Post by tenochtitlanuk on May 27, 2024 6:02:15 GMT -5
I think Anatoly missed a trick here. Depends on whether 'between' includes both limits... Run the following code, to see what happens with the upper limit.
EDIT
nomainwin
WindowWidth =500 WindowHeight =480
dim numbers( 40), numbers2( 40)
open "Rand demo" for graphics_nsb as #wg
#wg "trapclose quit"
#wg "down ; fill 120 120 80 ; size 9 ; color darkblue ; backcolor 120 120 80 ; font 7"
for k =0 to 40 #wg "set "; 10 +k *20; " 430 " #wg "up ; goto "; 10 +k *20; " 448 " #wg "down" #wg "\"; str$( k) next k
#wg "color red ; size 6"
for i =1 to 60000 X =randRange( 3, 20) X2 =IntRndInRange( 3, 20) numbers( X) =numbers( X) +1 numbers2( X2) =numbers2( X2) +1 #wg "color red ; line "; 8 +X *20; " 430 "; 8 +X *20; " "; 430 -numbers( X) /10 #wg "color green ; line "; 15 +X2 *20; " 430 "; 15 +X2 *20; " "; 430 -numbers( X2) /10 scan next i
#wg "flush"
wait
'***************************************************** 'function returns a random integer in range [Min, Max) function randRange( Min, Max) randRange = Min + random( Max -Min) end function
'function returns a random integer in range [0, n) function random(n) random = int( rnd( 1) *n) end function
function IntRndInRange( a, b) IntRndInRange =a +int( rnd( 1) *( 1 +b -a)) end function
sub quit h$ close #h$ end end sub
|
|
Jack Kelly
New Member
I see no benefit from anonymity.
Posts: 19
|
Post by Jack Kelly on May 27, 2024 7:37:05 GMT -5
You have to add 1 to the range.
|
|
|
Post by tsh73 on May 27, 2024 14:28:59 GMT -5
I would say it is strictly defined in comment
'function returns a random integer in range [Min, Max) so "[" means "included", ")" mens "not included" Code does exactly what.
Of cource that might be not that "between" usually means. I just checked - Oracle SQL has BETWEEN and it includes both ends.
But this stems from C habit of using N to get 0..N-1, and I think it is pretty common.
|
|