Follow via:
RSS
Email Alert
Question
0 Votes
+ -

How to generate random numbers in a text box.

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.
2nd May 2007

Answers (4)

0 Votes
+ -
The function is rnd
it returns a float >=0 =1

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

if my spotty memory of VB6 serves me correctly.
3rd May 2007

Replies

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)
rwidegren@... 4th May 2007
Forgot Randomize, just like I'm trying to forget VB6
Tony Hopkinson 9th May 2007
0 Votes
+ -
My article about VB random limitations
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
4th May 2007

Replies

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.
Tony Hopkinson 9th May 2007
Since 15seconds.com has gone dark, I've republished the article here:
http://www.experts-exchange.com/A_11114.html
aikimark@... 26th Jan
0 Votes
+ -
textbox unsuitable
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.
4th May 2007

Replies

some times putting every thing in text boxes gives you a more pleasant layout.
Tony Hopkinson 9th May 2007
0 Votes
+ -
How to produce random integers in a given range
dim max,min
max=100
min=1
Text1.Text = (Int((max - min + 1) * Rnd + min))


Output:

71
Updated - 7th May 2007
Answer the question
Formatting +
BB Codes - Note: HTML is not supported in forums
  • [b] Bold [/b]
  • [i] Italic [/i]
  • [u] Underline [/u]
  • [s] Strikethrough [/s]
  • [q] "Quote" [/q]
  • [ol][*] 1. Ordered List [/ol]
  • [ul][*] · Unordered List [/ul]
  • [pre] Preformat [/pre]
  • [quote] "Blockquote" [/quote]

Join the TechRepublic Community and join the conversation! Signing-up is free and quick, Do it now, we want to hear your opinion.