If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back  dBforums > Data Access, Manipulation & Batch Languages > Visual Basic > VBA - Randomize/Rnd issue with whole numbers.

LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Registered User
Join Date: Nov 2012
Posts: 72
VBA - Randomize/Rnd issue with whole numbers.

I'm having trouble getting a random number procedure to work in that I want it to display a random number between two numbers with the output displaying two decimal points. Such as:

Range: 7.35 - 7.45.

I've tried a couple of things like:

Int((Upperbound - Lowerbound + 1) * Rnd + Lowerbound)
, replacing the +1 with, say, .01, .1, 0.1, etc.

The issue is it always displays a whole number without decimals. Even if I have the textbox set to display 2 decimal points and a standard number it displays 7.00, 6.00, etc.

What am I doing wrong?

Thank you!
Reply With Quote
  #2 (permalink)  
Purveyor of Discontent
Join Date: Mar 2003
Location: The Bottom of The Barrel
Posts: 6,102
Integers are whole numbers. They can only ever represent whole numbers. If you try to cast a decimal to an integer, it will drop the fraction.

You want to be working with a decimal instead.
oh yeah... documentation... I have heard of that.

*** What Do You Want In The MS Access Forum? ***
Reply With Quote
  #3 (permalink)  
Jaded Developer
Join Date: Nov 2004
Location: out on a limb
Posts: 12,297
if your range is 7.35 - 7.45.
dim MyRandomNumber as double
MyRandomNumber = (Int((745 - 735 + 1) * Rnd + 735) )/ 100
Is taking part in the 'Distinguished Gentlemans' ride. Being neither distinguished nor a gentleman, but I am keen to raise funds for Prostrate Cancer research. At present I'm threatening to ride a Norton 961 wearing a kilt. If you would like to contribute to Prostrate Cancer research then please consider donating via this page:-
Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On