0

それぞれが事前定義された文字数を持つ、事前定義された数の行を作成しようとしています。使用できる文字は 0 と 1 のみで、ランダムに生成されます。

私はこのコードでこれを達成しました:

    Dim _allowedChars As String = "01"
    Dim randomNumber As New Random()
    Dim chars As List(Of Char) = New List(Of Char)
    Dim allowedCharCount As Integer = _allowedChars.Length
    For o As Integer = 0 To rows - 1
        For a As Integer = 0 To rowCharacters - 1
            chars.Add(_allowedChars.Chars(CInt(Fix((_allowedChars.Length) * randomNumber.NextDouble()))))
        Next a
        For a As Integer = 0 To chars.Count - 1
            'results for each row go here in a textbox line
        Next a
        chars = New List(Of Char)
        'row o finished, new line and go for next row
    Next o

これはうまく機能します。5 行と 5 文字 (ランダムに生成された 0 または 1 のみで構成される) を設定した出力例を次に示します。

11101
00000
11011
00000
01011

ここでさらにひねりを加えたいと思います: 各行の 1 の最小パーセンテージを指定します。つまり、「ランダムに分散されていても、各行には少なくとも 20% の 1 が含まれている必要があります」。パーセンテージは、各行の文字の長さに基づいています (コード内の変数 rowCharacters)。

これで私を助けてくれる人はいますか?

ありがとう!

4

1 に答える 1