random number but not number ?
Blitz3D Forums/Blitz3D Beginners Area/random number but not number ?
| ||
I need someone to help me write a function to return the number of random numbers specified, but they all need to be unique, the numbers it returns. Like what I was thinking was something like: type rnumber field rand1,rand2,rand3,rand4,rand5,rand6 end type global numbers.rnumber = new rnumber function MyRand(num_numbers%, min%, max%) ;maximum of 6 random numbers returned, prolly have to ;make a type shown above to return all of them at ;same time. .not_unique2 if num_numbers = 2 numbers\rand1 = rand(min,max) numbers\rand2 = rand(min,max) goto check_unique endif .check_unique if num_numbers = 2 then if numbers\rand1 = numbers\rand2 then goto not_unique2 else goto unique2 endif endif .unique if num_numbers = 2 then return numbers endif end function ;Something like above, with 2,3,4,5,6 different ;random numbers being returned depending on the need. The reason is, I am randomly placing a few puzzle pieces, and I don't want them to overwrite each others grid positions, so if a certain gridx is already selected, I don't want to pick it again, in the random routine. Same for gridy locations. Any help would be greatly appreciated. What I have written is kind of obfiscuated and not sure if it will work as designed or not. Thanks |
| ||
Learned this from a post by Douglas:SeedRnd(MilliSecs()) Dim pickarray(11) For a=0 To 11 pickarray(a)=a Next Dim gridarray(2,3) c=11 For a=0 To 2 For b=0 To 3 d=Rand(0,c) gridarray(a,b)=pickarray(d) pickarray(d)=pickarray(c) c=c-1 Next Next Print gridarray(0,0)+" "+gridarray(0,1)+" "+gridarray(0,2)+" "+gridarray(0,3) Print gridarray(1,0)+" "+gridarray(1,1)+" "+gridarray(1,2)+" "+gridarray(1,3) Print gridarray(2,0)+" "+gridarray(2,1)+" "+gridarray(2,2)+" "+gridarray(2,3) WaitKey() |