-
generate numbers
hallo all. my problem is ....
i created one application for generating random numbers and using a one module for generating from vb page with code.. so..
i created a textbox named txtbox3 and txtbox1 for input user defined number for start and stop generating numbers.
everytime a program generating numbers starts from " 1 " to user defined number.
where is a problem but no start generating from textbox input number.
i gave a sample code of program.
interface having a
one listbox named text2
two textboxes named text1 and text3
one button named Command1
code of Form1.vb
Dim sequence() As Integer
Private Sub Command1_Click()
Text2 = ""
If Not (IsNumeric(text3.Text)) Then
MsgBox ("Pleace Enter a number")
Exit Sub
End If
If Val(text3) = 0 Then Exit Sub
ReDim sequence(Val(text3.Text))
If (getnewsequence(sequence, Val(text3.Text))) Then
'Debug.Print " sequence generated OK"
Else
'Debug.Print " sequence NOT generated "
End If
Dim Text1 As Integer
For Text1 = Text1 To Val(text3.Text)
Text2.AddItem (Text2 & sequence(Text1))
Next
End Sub
Private Sub Form_Load()
Text1 = Text1.Text
End Sub
code of module ... module named a " modrndnumgen "
Dim uniqueseq() As Integer
Dim numberofnumbers As Integer
Public Function getnewsequence(sequence() As Integer, number As Integer) As Boolean
On Error GoTo error
ReDim sequence(number)
Dim Text1, newnum As Integer
numberofnumbers = number
ReDim shufseq(numberofnumbers + 1)
ReDim uniqueseq(numberofnumbers + 1)
newnum = Randomgen
uniqueseq(1) = newnum
sequence(1) = newnum
For Text1 = 2 To numberofnumbers
Do
newnum = Randomgen
Loop Until ((checkifexists(newnum)) = True)
uniqueseq(Text1) = newnum
sequence(Text1) = newnum
Next
getnewsequence = True
Exit Function
error:
getnewsequence = False
End Function
Private Function Randomgen() As Integer
On Error Resume Next
Randomize
Do
Randomgen = Int((numberofnumbers + 1) * Rnd)
Loop Until Randomgen <> 0
End Function
Private Function checkifexists(number As Integer) As Boolean
On Error Resume Next
Dim Text1 As Integer
For Text1 = 1 To numberofnumbers + 1
If number = uniqueseq(Text1) Then
checkifexists = False
Exit Function
End If
Next Text1
checkifexists = True
End Function
i dont no where is a problem may be a
numberofnumbers = 0
to starts from 0 generate
i try to change it and gives me errors
:(:(:(:(
-
Re: generate numbers
Can you edit your post and use [code] tags to display the code and indent it (if not indented yet) please? This is to hard to read.
-
Re: generate numbers
Alright, the problem is that the module is not accessing the form, but the local variables.
What you have to do is
- Remove all "On Error Resume Next" lines, because they make it harder to find errors in the code if they are in there
- In the module remove every "Dim Text1"
- Replace "Text1" in the module with "int(Form1.Text1.Text)"
- In checkifexists use a new variable for the loop
Example of the things above:
(RED = to remove)
(ORANGE = to edit)
Code:
Private Function checkifexists(number As Integer) As Boolean
On Error Resume Next
Dim v1 As Integer
For v1 = 1 To numberofnumbers + 1
If number = uniqueseq(int(Form1.Text1.Text)) Then
checkifexists = False
Exit Function
End If
Next v1
checkifexists = True
End Function
So at least remove all "On Error Resume Next" lines and if it still doesn't work, post the error you get.
-
Re: generate numbers
One thing for sure, you can prrobably replace this chunk of your code:
Code:
Do
Randomgen = Int((numberofnumbers + 1) * Rnd)
Loop Until Randomgen <> 0
with this
Code:
RandomGen = Int(Rnd * numberofnumbers + 1)
In your code, if numberofnumbers somehow reaches 0 or -1, your code will hang forever. My suggested code will produce 0 only if numberofnumbers reaches -1 and it will never hang.