Generating uniform random numbers in vb.net

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Generating uniform random numbers in vb.net



I have arraylist which contains numbers with array index in the following order:


0 - 10000
1 - 10500
2 - 11000
3 - 11500
.
.
99 - 59500



The logic here is that the starting numbers changes after the interval of 20. So i want to have atleast 1 number selected randomly from the 5 sets of 20 for any numbers of selection without repeating the numbers.



For example if i select 6 numbers randomly then the numbers can be


10500
20000
30500
40500
50000
40000



here i got all the starting numbers present in the selection without repeating
the numbers. if the number of selection is 10 then the selected numbers must contain 2 numbers from each 5 sets which means there will be 2 numbers starting with 1, 2 numbers starting with 2, 2 numbers starting with 3,.....All the selected numbers must be non repeating.



Can anyone help me to achieve this using VB.NET.
Heres my code and it works fine till it reaches around 50 then after that it goes int to inifinite loop. Please help me fix it.


Shared random As New Random
Dim Selected As New ArrayList()
Dim RSNo As Integer = 0
Dim RENo As Integer = 19
Dim countSelected As Integer = 0
Dim randomIndex As Integer
Dim i As Integer = 0

While i < totalNum
If i Mod 5 = 0 Then
RSNo = 0
RENo = 19
End If

For j As Integer = RSNo To RENo Step 1
If selected.Contains(j) Then
countSelected += 1
Else
Exit For
End If
Next

If countSelected = 20 Then
randomIndex = random.Next(0, 99)
While selected.Contains(randomIndex)
randomIndex = random.Next(0, 99)
End While
Else
randomIndex = random.Next(RSNo, RENo)
While selected.Contains(randomIndex)
randomIndex = random.Next(RSNo, RENo)
End While
End If

selected.Add(randomIndex)
i += 1
RSNo = RENo + 1
RENo += 20
countSelected = 0

End While





So, to be clear, you're saying that you have multiple intervals and you want to generate N random numbers from each interval without repeats, correct? If so, how would you generate N random numbers from one interval with no repeats? If you don't know that then you obviously haven't done any research because that's a commonly asked and answered question. All you have to do is that for each interval. Once you know how to make an omelette with on egg, you don't have to ask how to make an omelette with any other number of eggs. Same goes here.
– jmcilhinney
9 hours ago





If its like making an omlette then can you please share me how to do it because i searched but couldnt get what i was looking for
– Kinzang Dorji
9 hours ago





If I was correct in my interpretation and you didn't find any useful information then you need to put programming on hold for a bit and learn how to search the web. You're certainly not alone but it never ceases to amaze me how bad so many people are at doing a simple web search. I just searched for "vb.net generate unique random numbers" and the very first result was code example on this very site. Maybe language is a problem in some cases but I even expressed the concept in plain English in my first comment so why did you ask me to do your work for you instead of using that?
– jmcilhinney
9 hours ago




1 Answer
1



Constructs like While selected.Contains(randomIndex) ... choose another randomIndex are a recipe for a program which may not terminate in a reasonable time.


While selected.Contains(randomIndex) ... choose another randomIndex



First, see that the arraylist index maps to (10000 + 500 * index). So we can ignore that until it comes to presentation time.



Using intervals in the way you have written is commonly referred to as binning the numbers. So you want five bins.



You know how many numbers you need, and how many bins there are, so you need to distribute the selected numbers across the bins. Sometimes it will not be possible to distribute the selected numbers in equal quantities among the bins so some "lucky" bins will have one more to be selected from them.



Then you need a source of a range of numbers in a random order to make the selections; this is best created as a separate function.



To create the selection, go through the bins and select the appropriate quantity of random numbers from each bin.



With some refinements needed, this shows what I mean:


Option Infer On
Option Strict On

Module Module1
Dim rand As New Random()

''' <summary>
''' Create an unordered list of integers in the range [0, n).
''' </summary>
''' <param name="n">Upper limit, exclusive.</param>
''' <param name="rand">An instance of the Random class.</param>
''' <returns>List(Of Integer) from 0..n-1 in a random order.</returns>
<DebuggerStepThrough>
Function RandomList(n As Integer, rand As Random) As List(Of Integer)
Dim r As New List(Of Integer)(n)
For i = 0 To n - 1
r.Insert(rand.Next(0, i + 1), i)
Next

Return r

End Function

Function PickRandomValues(nValues As Integer) As List(Of Integer)
'TODO: Refactor nMin, nMax, nBins, rand as parameters.
'TODO: Detect impossible requirements.

Dim selection As New List(Of Integer)

Dim nMin = 0
Dim nMax = 99
Dim nBins = 5
Dim binSize = (nMax - nMin + 1) nBins
Dim numbersPerBin = nValues nBins
' the remainder of the items will be distributed randomly amongst the bins...
Dim extraNumbers = nValues Mod nBins
Dim luckyBins = RandomList(nBins, rand).Take(extraNumbers).ToList()

For bin = 1 To nBins
Dim binMin = nMin + (nMax - nMin + 1) * (bin - 1) / nBins
Dim binMax = nMin + (nMax - nMin + 1) * bin / nBins - 1

Dim numbersFromThisBin = numbersPerBin + If(luckyBins.Contains(bin - 1), 1, 0)
Dim picks = RandomList(binSize, rand).Take(numbersFromThisBin)

For Each p In picks
selection.Add((bin - 1) * binSize + p)
Next

Next

Return selection

End Function

Sub Main()
' Show example results...
For i = 1 To 10
Console.WriteLine(String.Join(", ", PickRandomValues(11).Select(Function(x) 10000 + 500 * x)))
Next

Console.ReadLine()

End Sub

End Module



Sample output:



10000, 17500, 21500, 26500, 20000, 32000, 35500, 43500, 49000, 53000, 57000
12000, 14000, 29000, 25000, 31500, 30000, 49000, 45000, 48000, 56500, 57000
15500, 12500, 22500, 23000, 31000, 38000, 36000, 45500, 46000, 59000, 58500
10000, 19000, 16500, 25500, 24000, 37500, 31500, 44500, 40000, 50000, 52500
16000, 11500, 26500, 24000, 38500, 39500, 36500, 44000, 43500, 53500, 57500
18500, 17000, 24500, 29000, 33000, 37000, 45000, 41000, 51000, 53500, 51500
10000, 16500, 12500, 27500, 24000, 34500, 33000, 43000, 45500, 55000, 53000
17000, 17500, 12000, 20500, 24000, 39000, 36500, 44000, 47000, 53000, 55000
10000, 17500, 24500, 27000, 30500, 32500, 49000, 46500, 48000, 57000, 50500
13000, 11500, 20000, 29500, 24000, 32500, 39500, 45500, 46500, 59000, 58500



N.B. Arraylists have been superseded by Lists.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Makefile test if variable is not empty

Will Oldham

'Series' object is not callable Error / Statsmodels illegal variable name