Jump to content

Visual Basic rnd help


FireBFM

Recommended Posts

What i have.

 

For x = 1 to 9

Num = Int(Rnd*3) + 1

Next X

theres 9 command boxes control array. each one will be assigned a random # 1 to 3. But i can only use 3 ones, 5 twos and 1 three. Help with bolded part

Edited by FireBFM
Link to comment
Share on other sites

What i have.

 

For x = 1 to 9

Num = Int(Rnd*3) + 1

Next X

theres 9 command boxes control array. each one will be assigned a random # 1 to 3. But i can only use 3 ones, 5 twos and 1 three. Help with bolded part

 

You could keep track of how many of each digit you have left and "reroll" if Num is a digit you've used up. I assume though that this isn't what you want, because the last boxes will tend to be a 2 more likely than the first boxes.

 

I'd probably just put the numbers (1,1,1,2,2,2,2,2,3) in an array and randomly select an index, and remove that choice from the array.

Link to comment
Share on other sites

Code please?

 

I don't know VB so consider this pseudocode...

 

dim Digits(0 to 8) as Integer
Digits(0) = 1
Digits(1) = 1
Digits(2) = 1
Digits(3) = 2
Digits(4) = 2
Digits(5) = 2
Digits(6) = 2
Digits(7) = 2
Digits(8) = 3

for x = 8 to 0 step -1
    ' select a random index from 0 to x
   Index = Int(Rnd * (x+1))
   Num = Digits( Index )

    ' Remove that digit from the array (effectively we're shrinking the array, and overwriting the used digit)
   Digits( Index ) = Digits( x )
next x

 

It might require some debugging.

Edited by md65536
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.