vb.net help random නම්බර් ඒකක්

vbDev

Well-known member
  • Feb 6, 2023
    1,410
    1,543
    113
    vb.net වලින් නම්බර් ඒකක් ජෙනරේට් කරගන්න ලේසි කොහොමද random ,

    උදා, 1- 100 නම්බර්ස් වලින් නම්බර් ඒකක් ජෙනරේට් වෙන්න ඔන..... ඒක නම්බර් ඒක ඒක සැරයක්... ඩෙටා බෙස් ඒකකට දාලා ආයේ ගන්න ඒකද කරන්න පුලුවන්...
     

    Ramiel

    Well-known member
  • Nov 21, 2015
    4,816
    11,813
    113
    https://stackoverflow.com/questions/18676/random-integer-in-vb-net

    Code:
    Public Function GetRandom(ByVal Min As Integer, ByVal Max As Integer) As Integer
        ' by making Generator static, we preserve the same instance '
        ' (i.e., do not create new instances with the same seed over and over) '
        ' between calls '
        Static Generator As System.Random = New System.Random()
        Return Generator.Next(Min, Max)
    End Function
     
    • Like
    Reactions: vbDev

    hasithayad

    Well-known member
  • Sep 28, 2011
    30,793
    1
    45,000
    113
    textboxes:
    txtCollectionView
    txtResult

    buttons:
    btnShuffle

    C#:
    Public Class Form1
    
        Dim RND As New Random()
        Dim Collection As Int32() = Enumerable.Range(1, 100).ToArray()
        Private Sub btnShuffle_Click(sender As Object, e As EventArgs) Handles btnShuffle.Click
            Shuffle(Collection)
            Dim selected As Integer
    
            If Collection.Length > 0 Then
                selected = Collection.Take(1).ToArray()(0)
                Collection = Collection.Skip(1).ToArray
                txtResult.Text = txtResult.Text & selected & vbCrLf
                UpdateCollectionView()
            Else
                MsgBox("There are no more items remaining.")
            End If
        End Sub
    
        Sub Shuffle(arry() As Integer)
            Dim tmp As Integer
            Dim j As Integer
    
            For n As Integer = arry.Length - 1 To 0 Step -1
                j = RND.Next(0, n + 1)
                tmp = arry(j)
                arry(j) = arry(n)
                arry(n) = tmp
            Next
        End Sub
    
        Sub UpdateCollectionView()
            txtCollectionView.Text = ""
            For Each item In Collection
                txtCollectionView.Text = txtCollectionView.Text & item.ToString & vbCrLf
            Next
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            UpdateCollectionView()
        End Sub
    End Class
     
    • Like
    Reactions: vbDev and EKGuest

    EKGuest

    Well-known member
  • Nov 16, 2022
    3,206
    5,703
    113
    ඩේටබේස් එකක් ඕනේ නෑ මචන් ජෙනරේට් කරන නම්බර් එක කලෙක්ශන් එකකට දාලා ආයේ ජෙනෙරේට් කරනකොට ඒ කලෙක්ශන් එකේ නම්බර් එක තියෙනවද බැලුවම හරි.

    මේ ෆන්ක්ශන් එක කෝල් කලාම එකත් සීයත් අතර රැන්ඩම් නම්බර් එකක් එනවා. එක නම්බර් එකක් එක පාරකට වඩා එන්නේ නෑ. නම්බර් සීයම ජෙනරේට් කලාට පස්සේ ආයෙ කෝල් කරන හැම සැරේටම එන්නේ -1.

    C#:
        Public Function GetNextRandomNumber() As Integer
    
            Static generatedNums As HashSet(Of Integer) = New HashSet(Of Integer)()
            Static generator As System.Random = New System.Random()
    
            If (generatedNums.Count = 100) Then
                Return -1
            End If
    
            Dim num As Integer
    
            num = generator.Next(1, 101)
            While (generatedNums.Contains(num))
                num = generator.Next(1, 101)
            End While
    
            generatedNums.Add(num)
    
            Return num
    
        End Function


    (මම VB.Net කරලා නෑ නෙට් එකෙන් සින්ටැක්ස් බලාගෙන හැදුවේ.)
     
    • Like
    Reactions: vbDev