Questions

How to generate random numbers in a text box.

+
0 Votes
Locked

How to generate random numbers in a text box.

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.
+
0 Votes
Tony Hopkinson

it returns a float >=0 <=1

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

if my spotty memory of VB6 serves me correctly.

+
0 Votes
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)

+
0 Votes
Tony Hopkinson

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

+
0 Votes
aikimark

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.

http://www.15seconds.com/issue/051110.htm

+
0 Votes
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.

+
0 Votes
aikimark

Since 15seconds.com has gone dark, I've republished the article here:
http://www.experts-exchange.com/A_11114.html

+
0 Votes
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.

+
0 Votes
Tony Hopkinson

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

+
0 Votes
mahalakshmi

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


Output:

71