Question Locked

How to generate random numbers in a text box.

0 helpful
10 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
Hello, I am new with Visual Basic 6.0 and I wanted to know how to generate random numbers. I am making a bowling game and I need to generate random numbers from 0-10. I want to show them when I click on the command button to throw the ball, the exact number of pins will disappear depending on the number that will appear in the text box. What I need now is the code to make random integers from 0-10. I only have one command button which is to throw the ball. So when I click on that command button which is named "Throw" I want a random number to appear.

10 replies

by tony hopkinson

it returns a float >=0 <=1

textbox1.text = cStr(Trunc(Rnd() * 11))

if my spotty memory of VB6 serves me correctly.

by rwidegren

The Randomize function seeds the rnd() function with the timer (if no other parameter is specified). You need to "re-seed" each time or you'll get the same sequence of numbers. Then you use the next statement to generate the random number - setting the upper and lower limits.

Dim iRand As Integer

Randomize
nRand = Int((MaxNum - MinNum + 1) * Rnd + MinNum)

by tony hopkinson

I've been aware that random wasn't random for along time. Seeing as it's an abstract mathematical concept with no real world equivalent. Chaos theory crapped on the entire idea.

I didn't know there was a specific weakness in the VB implementation though, can't say it's a surprise mind.

by t.bradford

I'd suggest you use a Label rather than a Textbox if you want to display your random number. A User could overtype your textbox resuls - not much point if that can happen. The label will display the result but not allow changes to be made.

by tony hopkinson

some times putting every thing in text boxes gives you a more pleasant layout.

by mahalakshmi

dim max,min
max=100
min=1
Text1.Text = (Int((max - min + 1) * Rnd + min))

Output:

71

This discussion is closed to new replies.