How to generate random numbers in a text box.

By ser710 ·
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.

This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Answers

Collapse -

The function is rnd

by Tony Hopkinson In reply to How to generate random nu ...

it returns a float >=0 <=1

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

if my spotty memory of VB6 serves me correctly.

Collapse -

Another example

by rwidegren In reply to The function is rnd

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

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

Collapse -

Well spotted.

by Tony Hopkinson In reply to Another example

Forgot Randomize, just like I'm trying to forget VB6

Collapse -

My article about VB random limitations

by aikimark In reply to How to generate random nu ...

There are some limitations on the VB pseudo-random number generation (PRNG) functions. I wrote the following article when one of my applications encountered a problem stemming from this limitation.

Collapse -


by Tony Hopkinson In reply to My article about VB rando ...

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.

Collapse -

Reponse To Answer

by aikimark In reply to My article about VB rando ...

Since has gone dark, I've republished the article here:

Collapse -

textbox unsuitable

by t.bradford In reply to How to generate random nu ...

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.

Collapse -

Just set readonly

by Tony Hopkinson In reply to textbox unsuitable

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

Collapse -

How to produce random integers in a given range

by mahalakshmi In reply to How to generate random nu ...

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



Related Discussions

Related Forums