Hi,
I want to generate an unpredictable random integer number but i dont know how. Below is my function to generate between (1 to 8)which keeps returning '6' on first run which is predicted and also, i dont want the numbers to repeat itself.

Private Function RandomIt(Hv As long, Lv As long) As long
RandomIt = Int((Hv - Lv + 1) * Rnd + Lv)
End Function

Please can someone help me out as i want to resume my work urgently.
Thank you.

Dani AI

Generated

had the classic VB issue: the simple Rnd-based approach can give a repeatable first value and an easy loop for uniqueness (as suggested by ) will become slow or even hang when you ask for more unique values than the range. The quick fixes people use (time-seeding and retrying until you get a new value) work for simple, non-security tasks, but they are still predictable and can be inefficient.

For true unpredictability use the OS cryptographic RNG and avoid time-seeding. In modern .NET you can call RandomNumberGenerator.GetInt32(min, maxExclusive) to produce unbiased integers directly from a secure source:

Private Function CryptoRandInt(min As Integer, maxInclusive As Integer) As Integer
    Return System.Security.Cryptography.RandomNumberGenerator.GetInt32(min, maxInclusive + 1)
End Function

If you need a non-repeating sequence within a run, build the full range and shuffle it once with Fisher–Yates, then consume elements sequentially. That guarantees no repeats until the list is exhausted and is O(n):

Private Function ShuffledRange(min As Integer, maxInclusive As Integer) As List(Of Integer)
    Dim n As Integer = maxInclusive - min + 1
    Dim arr = Enumerable.Range(min, n).ToArray()
    For i As Integer = n - 1 To 1 Step -1
        Dim j As Integer = System.Security.Cryptography.RandomNumberGenerator.GetInt32(0, i + 1)
        Dim tmp As Integer = arr(i)
        arr(i) = arr(j)
        arr(j) = tmp
    Next
    Return arr.ToList()
End Function

Notes and cautions: if you must avoid repeats across app restarts persist the shuffled list or the set of used values (file/DB). If targeting older frameworks where GetInt32 is unavailable, use RandomNumberGenerator/RNGCryptoServiceProvider with proper rejection sampling. For non-security, high-throughput cases a seeded System.Random is fine, but do not rely on time-only seeds when unpredictability is required.

References: RandomNumberGenerator API (docs.microsoft.com) and the Fisher–Yates shuffle (wikipedia).

Recommended Answers

All 2 Replies

Hi... if you want to get a special number on every start, try to add this line on first row of your fnc:

Randomize Timer

If you'd like to get unique numbers, e.g. for some series numerical, you probably have to make an array, a loop and shoud use it like this way:

Lv = 1
Hv = 8
Dim arrNumbers(Hv) As Boolean

'here comes some of your code where the function is used

Private Function RandomIt(Hv As Long, Lv As Long) As Long
Dim iX As Integer
Dim bExists As Boolean, bAdded As Boolean
    Randomize Timer
    Do Until bAdded = True
        RandomIt = Int((Hv - Lv + 1) * Rnd + Lv)
        For iX = Lv To Hv
            If arrNumbers(RandomIt) = True Then bExists = True: Exit For
        Next iX
        If bExists = False Then
            arrNumbers(RandomIt) = True
            bAdded = True
        End If
    Loop
End Function

You only have to take care about the counting the function uses. If you run it more times than the space between Lv and Hv is, it will end in an infinite loop... :-) And as you know, it takes a long time to do the infinite loop. My 486 does an infinite loop in 4,86 seconds :-)

Thanks mate, you are good!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.