0

I want to create a random number generator in VB.NET But from my own given list of numbers

Like Chose random numbers from [1,2,3,4,5,6] e.t.c

Ali Ahmad
  • 81
  • 2
  • 9
  • **XY Question Revealed!:** `I want to create a Serial Key Generator` A good serial number generator is not going to be based on random values. Random does not mean unique. For a serial number or key, you should hash something that identifies whatever you are registering and encode bytes from that - either Hex or a Base26 converter to give you results like `FKWF9-6LX4F-4RQ6Q-QD76D-3VYVR`. Deterministic GUID are also great, but the output is less user friendly. – Ňɏssa Pøngjǣrdenlarp Jan 05 '17 at 18:09

4 Answers4

3

This is how you get a random natural number in the interval of [0, n - 1]:

CInt(Rnd() * n)

Let's suppose you have a List of n elements. This is how you get a random element from it:

MyList(CInt(Rnd() * n))
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • Can You please explain some details LIKE from "123-123-123" and "234-234-234" – Ali Ahmad Jan 05 '17 at 17:15
  • @AliAhmad you have a list of items. There are n items in your list. If you randomize a natural number between 0 and n - 1, then you get a number, i, where 0 <= i <= n - 1. This number should be the index you will use to get the value from your list. – Lajos Arpad Jan 05 '17 at 18:38
  • > Can you please Answer this Question http://stackoverflow.com/questions/41540643/how-to-get-a-text-after-certain-text-in-vb-net – Ali Ahmad Jan 09 '17 at 03:55
  • @AliAhmad I will take a look at it. If this answer helped you with your question, then you might consider accepting it by clicking on the checkmark. – Lajos Arpad Jan 09 '17 at 05:21
  • Your question is not clear. You have a text, but where is that text? In a file? In a String? – Lajos Arpad Jan 09 '17 at 05:25
3

Already built into .NET base of 'Random' and then extending that into your existing choices. This is NOT the same as generating the number from a Random as you are specifying YOUR OWN list first and then merely getting positioning with the help of a new Rand and using your length as a ceiling for it.

 Sub Main()
    'Say you have four items in your list
    Dim ls = New List(Of Integer)({1, 4, 8, 20})
    'I can find the 'position' of where the count of my array could be
    Dim rand = New Random().Next(0, ls.Count)
    'This will give a different 'position' every time.
    Console.WriteLine(ls(rand))

    Console.ReadLine()
  End Sub
djangojazz
  • 14,131
  • 10
  • 56
  • 94
1

I would create a random number generator to generate a random number in the range of the list/array length, then use the result to point to the index of your number list.

Dim numbers As Integer() = New Integer() {1,2,5,6,7,8,12,43,56,67}

Dim randomKey = numbers(CInt(Rnd() * numbers.length))

*Edited based on Lajos Arpad's answer of how to get the random number

big_water
  • 3,024
  • 2
  • 26
  • 44
0

Here is the function you can try, see more here Random integer in VB.NET

Public Function GetRandom(ByVal Min As Integer, ByVal Max As Integer) As    Integer
Dim Generator As System.Random = New System.Random()
Return Generator.Next(Min, Max + 1)
End Function
Community
  • 1
  • 1
Tony Dong
  • 3,213
  • 1
  • 29
  • 32