I use the following function
CInt(Math.Floor((99999999 - 10000000 + 1) * Rnd())) + 10000000)
To get random numbers with length of 8 digits...
Ok I get sum of different numbers with the length I want when I run this function in debugging mode
But when I run my program that function
Dim rguun As String = CInt(Math.Floor((99999999 - 10000000 + 1) * Rnd())) + 10000000)
Always gives me back the same number
Why is that happen?
Public Sub randomnumber()
Dim rndNumber As String = ""
Dim rnd As New Random
For n As Integer = 0 To 7
rndNumber &= Rnd.Next(0, 9)
Next
End Sub
Related
This is a number #
This is a number or letter?
Separate the random string like ??#?#-???##-#?#???-#???#-##
I need some code that generates the string as shown above. It doesn't have to be complicated.
Expected result example: 2F421-QD421-2W3FY0-3F4L1-37
I've tried using PHP and this example but wasn't able to achieve what I was looking for Generating a random numbers and letters
I am looking for a vb.net project to handle the generation so i can submit the serial into a database manually.
I quite like this approach:
Dim characters = "0123456789ABCDEFGHIJKLOMNOPQRSTUVWXYZ"
Dim template = "??#?#-???##-#?#???-#???#-##"
Dim rnd = New Random()
Dim query =
From t In template
Select If(t = "-", "-", characters(rnd.Next(If(t = "?", characters.Length, 10))))
Dim result = String.Join("", query)
Console.WriteLine(result)
It gives me output like this:
RC2C9-DHB47-1Q07RL-8BIF7-57
Create 2 functions 1 for letters GRL (Generate Random Letter) 1 for numbers GRN (Generate Random Number) like so:
Result of what i called is: W96-GKlF6
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Console.WriteLine(GRL(1) + GRN(2) + "-" + GRL(4) + GRN(1))
End Sub
Public Function GRL(ByRef iLength As Integer) As String
Static rdm As New Random()
Dim allowChrs() As Char = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ".ToCharArray()
Dim sResult As String = String.Empty
For i As Integer = 0 To iLength - 1
sResult += allowChrs(rdm.Next(0, allowChrs.Length))
Next
Return sResult
End Function
Public Function GRN(ByRef iLength As Integer) As String
Static rdm As New Random()
Dim allowChrs() As Char = "0123456789".ToCharArray()
Dim sResult As String = String.Empty
For i As Integer = 0 To iLength - 1
sResult += allowChrs(rdm.Next(0, allowChrs.Length))
Next
Return sResult
End Function
Easy, random numbers to use as ASCII codes, then check the position to delimit if its going to be just a number or a character that can be number or letter.
When is a position that can be number or letter, analyze the random number and split it. If the number is less than 11 that means is a number then add 47 and use the result as ASCII code (random create numbers from 1 to 36) so for example if the random is 1, we say 47 + 1 = 48, 48 is the ASCII code of 0.
If the number is 11 or more we add 54, so for example if random is 11 then we have 11 + 54 = 65. 65 is the ASCII code for the letter A.
Dim Key As String = ""
Dim N As Integer
Randomize()
For t = 1 To 23
If t = 3 Or t = 5 Or t = 9 Or t = 10 Or t = 11 Or t = 13 Or t = 17 Or t >= 21 Then
N = 10
Else
N = 36
End If
Dim value As Integer = CInt(Int(N * Rnd() + 1))
If value < 11 Then
Key = Key & Chr(value + 47)
Else
Key = Key & Chr(value + 54)
End If
If t = 5 Or t = 10 Or t = 16 Or t = 21 Then
Key = Key & "-"
End If
Next
How to insert another instruction to be able to avoid repetition of number as all the six numbers should be different from each other?
Here is the code of my Visual Basic module:
Module Loto
Sub Main()
Dim Value1, Value2, Value3, Value4, Value5, Value6 As Integer
Console.WriteLine("Example 3: To generate six numbers from 1 to 40")
Console.WriteLine()
'Initialize the random-number generator.
Randomize()
'Generate a random value between 1 and 40.
'Int((HighestValue - LowestValue + 1) * Rnd) + LowestValue
Value1 = CInt(Int((40 * Rnd()) + 1))
Value2 = CInt(Int((40 * Rnd()) + 1))
Value3 = CInt(Int((40 * Rnd()) + 1))
Value4 = CInt(Int((40 * Rnd()) + 1))
Value5 = CInt(Int((40 * Rnd()) + 1))
Value6 = CInt(Int((40 * Rnd()) + 1))
Do While (Value1 = Value2)
Value2 = CInt(Int((40 * Rnd()) + 1))
Loop
Console.WriteLine("Random number generated is " & Value1)
Console.WriteLine("Random number generated is " & Value2)
Console.WriteLine("Random number generated is " & Value3)
Console.WriteLine("Random number generated is " & Value4)
Console.WriteLine("Random number generated is " & Value5)
Console.WriteLine("Random number generated is " & Value6)
Console.ReadLine()
End Sub
End Module
I like the direction Plutonix mentioned, i.e. using Random class, yet it's possible to make it even shorter:
Dim r As New Random
Dim a = Enumerable.Range(1, 40)
Dim b = a.OrderBy(Function() r.Next).Take(6)
My $.02
Private Shared prng As New Random
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim a As List(Of Integer) = (From x In Enumerable.Range(1, 40)
Order By prng.Next Take 6).ToList
For Each n As Integer In a
Debug.WriteLine(n)
Next
End Sub
.NET provides for a better random generator than the legacy VB one. Basically, you want to create an array of values and swap them so that no number repeats.
This is called a shuffle since you are not actually picking random numbers but putting a set of values (like a deck of cards or a series of numbers) into a random order.
Dim rValues(39) As Integer ' random values holder
Dim rand = New Random() ' NET random generator - do this ONCE
Dim temp As integer
Shuffle an array of integers:
For ndx As Int32 = rValues.Length - 1 To 0 Step-1
Dim r = rand.Next(n + 1) ' next value
temp = rValues(ndx) ' swap values
rValues(ndx) = rValues(r)
rValues(r) = temp
Next
If used for something like a deck of cards, pick values starting at rValues(0) until you get to the end. To get just 6, take the first ones there wont be any repeats.
As an in-place shuffle, it is very efficient.
create an array 1..40, get a random number 1..40, exchange the value of that position with the value at pos(40), get a random number 1..39 and exchange that value with pos(39) ... in the end you have the numbers at pos 35..40
create an array for your answers and check it after every round:
dim resultArr(6) as integer
dim unique as boolean
for i=0 to 5
unique=false
while not unique
resultArr(i)=CInt(Int((40 * Rnd()) + 1))
for j=0 to i-1
unique=(unique OR resultArr(j)<>resultArr(i))
next
wend
next
I recently wrote this program in Visul Basic 13.
it searchs for the nth catalan number but after 48 even Decimal type is too short.
Is there any other way to represent them? like in the form of A*10^n?
Public Class Try_Catalan_Number
'Catalan numbers form a sequence of natural numbers that occur in various counting problems,
'often involving recursively defined objects.
Inherits Base_Number
Public Overrides Sub Test()
Dim Return_Catalan_Value As Decimal
If Function_Catalan(Return_Catalan_Value) = False Then
Return_To_Form_Boolean = False
Else
Return_To_Form_Boolean = True
End If
Return_To_Form_Value = Function_Catalan(Return_Catalan_Value)
End Sub
Private Function Function_Catalan(Return_Catalan_Value As Decimal) As Decimal
'We return a Decimal function because catalan numbers can be very big and decimal is the biggest type.
Dim Binomial_Cofficients As Decimal
Dim Result As Decimal
Dim Number_Of_Loops As Integer
Dim tmpNumber As Object
Dim K As Decimal
Dim N As Decimal
If (Number > 48) Then
Return False
Exit Function
End If
'48 is the largest catalan number position which can be displayed...any position above 48 is too big.
tmpNumber = Number - 1
N = 2 * tmpNumber
K = tmpNumber
Result = 1
For Number_Of_Loops = 1 To K
Result = Result * (N - (K - Number_Of_Loops))
Result = Result / Number_Of_Loops
Next Number_Of_Loops
Binomial_Cofficients = Result
tmpNumber = Number - 1
tmpNumber = ((1 / (1 + tmpNumber)) * Binomial_Cofficients)
Return_Catalan_Value = tmpNumber
Return Return_Catalan_Value
End Function
End Class
[I assume by "Visul Basic 13" you mean the VB which is associated with Visual Studio 2013, i.e. VB version 12.0.]
You can use System.Numerics.BigInteger (you'll have to add a reference to System.Numerics):
Imports System.Numerics
Module Module1
Friend Function Factorial(n As Integer) As BigInteger
If n < 2 Then Return 1
If n = 2 Then Return 2
Dim f As BigInteger = BigInteger.Parse("2")
For i = 3 To n
f *= i
Next
Return f
End Function
Friend Function CatalanNumber(n As Integer) As BigInteger
Return Factorial(2 * n) / (Factorial(n + 1) * Factorial(n))
End Function
Sub Main()
For i = 0 To 550
Console.WriteLine(CatalanNumber(i).ToString())
Next
Console.ReadLine()
End Sub
End Module
I did not test to see the maximum Catalan number it can calculate, and I have no inclination to verify the results beyond those shown on the Wikipedia page.
Optimisations are left as an exercise for the reader ;)
Edit: FWIW, I can get it to run a bit faster by using
Function CatalanNumber(n As Integer) As BigInteger
Dim nFactorial = Factorial(n)
Dim twonFactorial = nFactorial
For i = (n + 1) To 2 * n
twonFactorial = BigInteger.Multiply(twonFactorial, i)
Next
Return twonFactorial / (BigInteger.Pow(nFactorial, 2) * (n + 1))
End Function
The speed increase varies from roughly 50% (n=50) to 20% (n=5000). If you're only using the function a few times for fairly small n, there may be little point worrying about it.
Edit2 Re-writing your function a bit to make it easier to read and removing the off-by-one error, we get:
Private Function Function_Catalan(a As Integer) As BigInteger
If a = 0 Then Return 1
Dim binomialCofficient As BigInteger = BigInteger.One
Dim n As Integer = 2 * a
Dim k As Integer = a - 1
For i As Integer = 1 To k
binomialCofficient = binomialCofficient * (n - (k - i)) / i
Next i
Return binomialCofficient / a
End Function
to get this format you could use:
String.Format("{0:E4}", InputNumber)
Start:
Randomize()
'randomValue = CInt(Math.Floor((upperbound - lowerbound + 1) * Rnd())) + lowerbound
Dim value As Integer = CInt(Int((2 * Rnd()) + 1))
intNumber = value - 1
Dim y As Integer
For y = 0 To 1
' Check arrNumber (y)
'If intnumber has already been selected,
'Then go and select another one.
If intNumber = arrNumber(y) Then
GoTo Start
End If
Next y
im am getting same value of value variable again
As you have posted an incomplete and non-working code sample, it's difficult to tell where your problem is.
I reduced your code to the core:
Option Explicit
Sub Randomize()
Dim value As Integer
Dim i As Integer
For i = 1 To 100
value = CInt(Int((2 * Rnd()) + 1))
Debug.Print value
Next i
End Sub
This code randomly prints the values 1 and 2 to the Immediate window (press CTRL-G to open it). The small range [1, 2] is due to the scaling with the value 2. It could easily be increased.
So Rnd() seems to just work fine.
Public Function random_key(ByVal lenght As Integer) As String
Randomize()
Dim s As New System.Text.StringBuilder("")
Dim b() As Char = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".ToCharArray()
For i As Integer = 1 To lenght
Randomize()
Dim z As Integer = Int(((b.Length - 2) - 0 + 1) * Rnd()) + 1
s.Append(b(z))
Next
Return s.ToString
Console.WriteLine(s.ToString)
End Function
i wanna print it like
s=textbox1.text or somethhing...
What wrong with using Console.WriteLine?
Note that you should not put it behind the return statement in the method because code behind a return is unreachable.
Dim random As String = random_key(10)
Console.WriteLine(random)