Jump to content

Visual Basic rnd help

Featured Replies

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

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.

  • Author

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.

 

Code please?

random_number.png

The translation to Visual Basic and the extension to "9 command boxes control array" (whatever that is) should be straightforward.

Edited by timo

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

  • Author

For X = 1 To 9

Num = (X - 1) \ 4 + 1

N = Int(Rnd * 9) + 1

cmdTile(N).Caption = Num

Next X

N has duplicates. So some of the tiles are blank. Help!

Use a list and remove each item as it is used! So populate a list with 3-1's, 5-2's, and a 3, and then randomly pick from the list removing the picked item each time.

Archived

This topic is now archived and is closed to further replies.

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.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.