HEELPBOOK - How to generate a number between 2 other numbers (Visual Basic) ##################### The question is: "How can I generate a number between 2 other numbers?" The main problem with this is that many people haven't realised that the Rnd function returns a value less than 1 BUT greater than or equal to zero (0<=Rnd<1). So since most people think that the Rnd function can generate the number 1, the algorithms they use are incorrect. The correct way is this: Int((upperbound - lowerbound + 1) * Rnd + lowerbound) Which can easily be put into a function: Private Function RandomInteger(Lowerbound As Integer, Upperbound As Integer) As Integer RandomInteger = Int((Upperbound - Lowerbound + 1) * Rnd + Lowerbound) End Function ...and an example of using it is: Private Function RandomInteger(Lowerbound As Integer, Upperbound As Integer) As Integer RandomInteger = Int((Upperbound - Lowerbound + 1) * Rnd + Lowerbound) End Function Private Sub Form_Load() Randomize 'Just once to start getting random numbers MsgBox RandomInteger(1, 52) End Sub The code above would be used most of the times for a card game, to draw a card at random. If we set Lowerbound=1 and Upperbound=6 then we would have ourselves dice rolling code. ############ ARTICLE INFO ############# Article Month: November Article Date: 13/11/2012 Permalink: http://heelpbook.altervista.org/2012/how-to-generate-a-number-between-2-other-numbers-visual-basic/ Source: http://www.vbforums.com/showthread.php?281172-Tutorial-Random-Numbers-VB6-and-earlier Language: English View more articles on: http://www.heelpbook.net/ Follow us on Facebook: http://it-it.facebook.com/pages/HeelpBook/100790870008832 Follow us on Twitter: https://twitter.com/#!/HeelpBook Follow us on RSS Feed: http://feeds.feedburner.com/Heelpbook Follow us on Delicious: http://delicious.com/heelpbook Linkedin: http://it.linkedin.com/pub/stefano-maggi/27/73a/b20 Google+ : https://plus.google.com/116990277568167008289/posts