Generating a random word from a list that is dependant on previous selections

DariaLoveborn

New Member
Joined
Apr 17, 2020
Messages
13
Office Version
  1. 365
Platform
  1. Windows
Hello,

This is the first time I've ever posted on a forum but I've scoured the internet for a solution to no avail.

I am creating a character generator and require it to randomly select a skill from a list that is dependant on the character level and also previous skills selected.

(Ignore the data in column O for now, it was a test) I want it to randomly select 2 skills from the LVL1 section of column M. Then, In column P I want it to randomly select 2 skills from the LVL1 and LVL2 sections of M without returning any of the selections from previous results.

I'm semi familiar with VBA and with clear instruction I can pretty much put anything together. I just can't figure this out.

Please help.

Thanks

Jen

Capture.PNG
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
@DariaLoveborn If I understand correctly then, maybe something like the below
Assumes that your data in image is effectively starting at row 2.

VBA Code:
Sub GetSkills()
Dim Arry() As Variant
Dim N As Long
Dim Temp As Variant
Dim J As Long


Range("P2:P5").ClearContents
   Arry = Range("M2:M5").Value
    Randomize
    For N = LBound(Arry, 1) To UBound(Arry, 1)
        J = CLng(((UBound(Arry, 1) - N) * Rnd) + N)
        If N <> J Then
            Temp = Arry(N, 1)
            Arry(N, 1) = Arry(J, 1)
            Arry(J, 1) = Temp
        End If
    Next N
  
    Cells(2, "P") = Arry(1, 1)
    Cells(3, "P") = Arry(2, 1)

    Arry = Range("M2:M13")
      Randomize
    For N = LBound(Arry, 1) To UBound(Arry, 1)
        J = CLng(((UBound(Arry, 1) - N) * Rnd) + N)
        If N <> J Then
            Temp = Arry(N, 1)
            Arry(N, 1) = Arry(J, 1)
            Arry(J, 1) = Temp
        End If
    Next N
  
    L = 4
    For N = LBound(Arry, 1) To UBound(Arry, 1)
        If Arry(N, 1) <> Cells(2, "P") And Arry(N, 1) <> Cells(3, "P") And Arry(N, 1) <> Cells(4, "P") Then
        Cells(L, "P") = Arry(N, 1)
        L = L + 1
      
        End If
        If L = 6 Then Exit For
    Next N
      
End Sub

Hope that helps.
 
Upvote 0
Yes. It's not in itself specifying a particular sheet so right click the sheet in question > view code, then paste it into the code pane for that sheet and run it with that sheet selected.
 
Upvote 0
Thank you, if I wanted it t do the same thing up to skill 15 could I just copy and paste and then amend the earlier parts of the code?
 
Upvote 0
Maybe not quite that simple. I would need to consider properly.
Are you always wanting to go through all the 15 levels. Or is it a progressive thing?
Is it always going to be two more skills per level, including the new skills and any unused from prior levels?
 
Upvote 0
Hiya yes it is. Always 2 extra skills per level including previous unused.

The code works currently just needs extending.

By the way you are a lifesaver!!! This has been frustrating me for about 6 hours now.
 
Upvote 0
I will probably not be able to look at this now until tomorrow. What is the reasoning for progressing downwards rather than randomising the whole list?
Is it so that the list you create relates to levels, two skills/ rows at a time?
 
Upvote 0
That's exactly it. Ordinarily a character will pick 2 skills at each level. They can add skills from a lover level to a higher level spot but not the other way around. I really appreciate the help with this. I love learning new things so this is brilliant. Tomorrow is perfectly fine. Thank you so much for the help.

Jen
 
Upvote 0
Jen, I have been playing with this and despite a bit of frustration here and there, I think this might be it.

Book1
LMNOP
1
2Lvl1Field DressingLong One Handed
3Mana BandMana Band
4Long One HandedCleave
5BastardBastard
6Lvl2Field DressingHealing Song
7Mana BandBasic Ground Trap
8Long One HandedField Dressing
9BastardPadded Armour
10Mana BandDart/Shot Specialisation
11Mana BandBuckler Shield
12CleaveShort Polearm
13Basic Ground TrapWay Lay
14Lvl3Healing SongPoison Weapon
15Padded ArmourLight Armour
16Lvl4Poison WeaponWords Of Clarity
17Thrown WeaponsFlight Weapons
18Dart/Shot SpecialisationThrown Weapons
19Lvl5Light ArmourHideous Laughter
20Mana BandMedium Sheild
21Words Of ClarityMedium Armour
22Buckler ShieldBardic Inspiration
23Lvl6Way LayBand Of Brothers
24Short PolearmMagical Law
25Mana BandShank
26Lvl7Hideous Laughter
27Mana Band
28Lvl8Flight Weapons
29Mana Band
30Lvl9Mana Band
31Lvl10Medium Armour
32Medium Sheild
33Lvl12Band Of Brothers
34Bardic Inspiration
35Lvl15Shank
36Magical Law
37Lvl18Taunt
Sheet7


VBA Code:
Sub GetSkills2()
Dim SklArry(2 To 36) As Variant
Dim PicArry() As Variant
Dim SklRes As Range

Dim N As Long
Dim Temp As Variant
Dim J As Long
Dim r, c, B, LastRow As Integer
    
Set SklPics = Range("P2:P25") 'Result Range of random picks
SklPics.ClearContents
'put All skills Level 1 thro' 15  to array
For r = 2 To 36
    SklArry(r) = Cells(r, "M")
Next r

'Make successive picks of two from each level and balance of previous unpicked skills
'level 1 thro 15 is 12 sections
For c = 1 To 12
    'Get last row of latest level
    LastRow = Application.WorksheetFunction.Choose(c, 5, 13, 15, 18, 22, 25, 27, 29, 30, 32, 34, 36)
    'put 'available' skills into pick array
    'NB Previously picked skills will be cleared, set to ""
    ReDim PicArry(1 To 35)
    B = 0
    For r = 2 To LastRow
        If Not SklArry(r) = "" Then
            B = B + 1
            PicArry(B) = SklArry(r)
        End If
    Next r
    ReDim Preserve PicArry(1 To B)
    
    'Random shuffle the current set of available skills
    
    Randomize
        For N = LBound(PicArry) To UBound(PicArry)
            J = CLng(((UBound(PicArry) - N) * Rnd) + N)
            If N <> J Then
                Temp = PicArry(N)
                PicArry(N) = PicArry(J)
                PicArry(J) = Temp
            End If
        Next N
            
    'Put picks 1 and 2 to the result list
    Cells(c * 2, "P") = PicArry(1)
    'Ensure pick 2 is not a duplicate of pick 1
    x = 2
    Do While PicArry(x) = PicArry(1)
        x = x + 1
    Loop
    
    Cells(c * 2 + 1, "P") = PicArry(x)
    
    'remove the picked skills from the Skills array
    For r = 2 To 36
        If SklArry(r) = PicArry(1) Or SklArry(r) = PicArry(x) Then SklArry(r) = ""
    Next r
Next c
    
End Sub

Hope that helps.
 
Upvote 0

Forum statistics

Threads
1,215,133
Messages
6,123,231
Members
449,091
Latest member
jeremy_bp001

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top