Question random numbers

instantwin

New member
Joined
Nov 2, 2009
Messages
4
Programming Experience
Beginner
is there a way to generate a random number and then display that number?

like for example i want to say "i got # - # pencils" or something like that

sorry i just started programming :cool:
 
Randomiser

Try this function for a random number with a specified amount of digits:

VB.NET:
    Public Function Normal(ByVal Length As Integer)
        Dim strInputString As String
        Dim intLength As Integer
        Dim intNameLength As Integer
        Dim intRnd As String
        Dim strName As String
        Dim intStep As Integer
        strInputString = "1234567890" 'these are the characters which will be in the password
        intLength = Len(strInputString)
        intNameLength = Length
        Randomize() ' just to make it random :D
        strName = ""
        For intStep = 1 To intNameLength
            intRnd = Int((intLength * Rnd()) + 1)
            strName = strName & Mid(strInputString, intRnd, 1)
        Next
        Normal = strName
    End Function

Or this code for a random number between two values:

VB.NET:
  Public Function RandomNumber()
        RandomNumber = Int((1000 - 1 + 1) * Rnd()) + 1
    End Function
(Generates a number between 1 and 1000
 
Try this function for a random number with a specified amount of digits:

VB.NET:
    Public Function Normal(ByVal Length As Integer)
        Dim strInputString As String
        Dim intLength As Integer
        Dim intNameLength As Integer
        Dim intRnd As String
        Dim strName As String
        Dim intStep As Integer
        strInputString = "1234567890" 'these are the characters which will be in the password
        intLength = Len(strInputString)
        intNameLength = Length
        Randomize() ' just to make it random :D
        strName = ""
        For intStep = 1 To intNameLength
            intRnd = Int((intLength * Rnd()) + 1)
            strName = strName & Mid(strInputString, intRnd, 1)
        Next
        Normal = strName
    End Function

Or this code for a random number between two values:

VB.NET:
  Public Function RandomNumber()
        RandomNumber = Int((1000 - 1 + 1) * Rnd()) + 1
    End Function
(Generates a number between 1 and 1000
I would suggest actually not using that code. Randomize and Rnd are holdovers from VB6. In VB.NET the Random class is more user-friendly and more consistent, so you should use that, as suggested by newguy.
 
okay i tried the it and it works when the number is by itself but when i include other stuff i get this error:

Option Strict On disallows implicit conversions from 'String' to 'Double'.

and it underlines the "i have"

my script is:

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim RandomClass As New Random()
Dim RandomNumber As Integer
RandomNumber = RandomClass.Next(10)

ListBox1.Items.Add("i have " + RandomNumber + " pencils")
End Sub
End Class

what does this mean and how do i fix it? :confused:
 
okay i tried the it and it works when the number is by itself but when i include other stuff i get this error:

Option Strict On disallows implicit conversions from 'String' to 'Double'.

and it underlines the "i have"

my script is:

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim RandomClass As New Random()
Dim RandomNumber As Integer
RandomNumber = RandomClass.Next(10)

ListBox1.Items.Add("i have " + RandomNumber + " pencils")
End Sub
End Class

what does this mean and how do i fix it? :confused:

This is why you should use the string concatenation operator (&) to concatenate strings rather than the addition operator (+). Addition works when both operands are strings but not otherwise. If you want to join strings use & all the time and you won't have an issue.
 
This is why you should use the string concatenation operator (&) to concatenate strings rather than the addition operator (+). Addition works when both operands are strings but not otherwise. If you want to join strings use & all the time and you won't have an issue.

it works. thanks :cool:
 
Back
Top