Hi guys,

Been an IT student for a few months now, and I've been kinda struggling along until now, but my last assignement I can't do. I don't expect anybody to do my homework for me, but I do want to just be able to understand the code rather then just copying someone elses....

I have to create a number generator, that generators 10 numbers between 1 and 9 randomly.....I've been staring at the example but I just don't get it. the RND function and all that, I just don't get any of it, and all the code like:

For iter = 1 to 6
num = int(rnd * 42 +1)
txtOutput.text = txtOutput.text + Str(num)
Next iter

I just don't get any of it, what any of it means, this is due very son, and my book on VB isn't arriving for a week!! :eek:

Thanks...

Recommended Answers

All 3 Replies

Private Sub cmdCalc_Click()
Rem clear previous entries
txtOutput.Text = ""
Rem generate new numbers each time
Randomize Timer
For iter = 1 To 9
num = Int(Rnd * 9 + 1)
txtOutput.Text = txtOutput.Text + Str(num)
Next iter
End Sub
Private Sub cmdExit_Click()
End
End Sub

This is what I have so far....opinions? It is right, or wrong? This is just the code, but I still don't genuinely understand it....

Also, how would I make one to give me numbers between 5 and 24, rather then 1 and any other number? That i really can't figure...

OK, let's look at your code (formatted)

Private Sub cmdCalc_Click()
    Rem clear previous entries
    txtOutput.Text = ""
    Rem generate new numbers each time
    Randomize Timer      '' Don't need to use Timer
    For iter = 1 To 9
        num = Int(Rnd * 9 + 1)  '' your code
                '' Rnd returns a value from 0 to .999999 (almost 1)
                '' That number * 9 gives you 0 to almost 9 (8.9999...)
                '' convert to int to get rid of the decimal portion (0-8)
                '' add 1 to get 1-9
        num = Int(Rnd * 9) + 1  '' corrected
        txtOutput.Text = txtOutput.Text + Str(num)
    Next iter
End Sub

Also, how would I make one to give me numbers between 5 and 24, rather then 1 and any other number? That i really can't figure...

The difference between 5 and 24...

rndMax = 24
rndMin = 5
num = Int(Rnd * (rndMax - rndMin)) + rndMin

Note the parentheses to make sure all the values are grouped properly.

Thank you Walt, I appreciate it very much, thanks for your time. :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.