VBA:将随机数添加到网格中不存在的网格中

时间:2016-04-22 14:07:52

标签: excel vba excel-vba random magic-square

package Se.lucas.Main;

public enum moneyTypes implements moneyType {

DOLLAR(moneyType, 15, "Dollar"),
 EURO(moneyType, 15, "Dollar"), 
  FRANK(moneyType, 15, "Dollar"),
   MARK(moneyType, 15, "Dollar"),
    POUND(moneyType, 15, "Dollar");

private moneyType type;
private int amount;
private String moneyName;

moneyTypes(moneyType type, int amount, String name) {
    type = this.type;
    amount = this.amount;
    name = this.name();
}


@Override
public int getMoney() {
    return this.amount;

}

@Override
public String getMoneyName() {
    return this.name();
}

@Override
public String getMessage() {
    return "got the message";
}

}

package Se.lucas.Main;

public interface moneyType {


public int getMoney();
public String getMoneyName();
public String getMessage();

}

我正在尝试做一个功课问题的一部分,要求在 VBA 中的魔术广场填补缺失的空格。它被设置为Sub FWP() Dim i As Integer Dim j As Integer Dim n As Integer n = Range("A1").Value For i = 1 To n For j = 1 To n If Cells(i + 1, j) = 0 Then Cells(i + 1, j).Value = Int(((n ^ 2) - 1 + 1) * Rnd + 1) ElseIf Cells(i + 1, j) <> 0 Then Cells(i + 1, j).Value = Cells(i + 1, j).Value End If Next j Next i 矩阵,其中包含n ^ 2个数字;我需要填充的空间由矩阵中的零表示。到目前为止,我有一些代码通过检查每个单独的单元格值,如果不是0,将保留值,如果值为0,则用1和n ^ 2之间的随机数替换它们。问题是,显然我得到了一些重复的值,这是不允许的,每个数字必须只有1个。

如何对其进行编码,以便网格中不会出现重复的数字? 我试图设置一个检查功能,看看它们是否已经在网格中,但我不知道该怎么做

由于

2 个答案:

答案 0 :(得分:1)

你可以采取很多方法,但是@CMArg说的是,数组或字典是确保你没有重复的好方法。

您要避免的是每个单元格逐渐变长以填充的情况。对于非常小的正方形(例如10x10)来说,这不是问题,但是非常大的正方形会变得难看。 (如果您的范围是1-100,并且除了31之外的所有数字都已经在表中,那么它需要很长时间 - 平均100次猜对,对吗? - 拉出一个未使用的数字。范围是1-40000(200x200),填充最后一个单元需要40000次猜测。)

因此,不要保留已经使用过的数字列表,而应考虑如何有效地完成并且&#34;交叉&#34;已经使用过的数字,以便每个新单元格正好采用1&#34;猜测&#34;填充。

以下是您可以实施的一种方式:

类:SingleRandoms

Option Explicit

Private mUnusedValues As Scripting.Dictionary
Private mUsedValues As Scripting.Dictionary

Private Sub Class_Initialize()
  Set mUnusedValues = New Scripting.Dictionary
  Set mUsedValues = New Scripting.Dictionary
End Sub

Public Sub GenerateRange(minimumNumber As Long, maximumNumber As Long)
  Dim i As Long
  With mUnusedValues
    .RemoveAll
    For i = minimumNumber To maximumNumber
      .Add i, i
    Next
  End With
End Sub

Public Function GetRandom() As Long
  Dim i As Long, keyID As Long
  Randomize timer
  With mUnusedValues
    i = .Count
    keyID = Int(Rnd * i)
    GetRandom = .Keys(keyID)
    .Remove GetRandom
  End With
  mUsedValues.Add GetRandom, GetRandom
End Function

Public Property Get AvailableValues() As Scripting.Dictionary
  Set AvailableValues = mUnusedValues
End Property

Public Property Get UsedValues() As Scripting.Dictionary
  Set UsedValues = mUsedValues
End Property

实际上的课程示例:

Public Sub getRandoms()
Dim r As SingleRandoms
Set r = New SingleRandoms
With r
  .GenerateRange 1, 100
  Do Until .AvailableValues.Count = 0
    Debug.Print .GetRandom()
  Loop
End With
End Sub

使用集合实际上比使用字典更有效,速度更快,但字典可以更容易地验证它是否正在做它应该做的事情(因为你可以使用{{ 1}}等。)。

答案 1 :(得分:0)

没有人会为你做功课。你只会欺骗自己。如果他们这样做,就会感到羞耻。

我不确定老师是多么挑剔,但有很多方法可以解决这个问题。

您可以将矩阵的值放入数组中。 检查零值元素是否存在,如果不存在,则中断。 然后获取插入的潜在随机数。 使用for循环遍历数组,检查每个元素的值。如果不存在,请替换零元素。