VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



If you store a random number and then display the number it, when displaying the random number, w

by Spencer Voice (1 Submission)
Category: Miscellaneous
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Fri 21st April 2000
Date Added: Mon 8th February 2021
Rating: (1 Votes)

If you store a random number and then "display" the number it, when displaying the random number, will sometimes show a value greater than

API Declarations


Dim dRandNumFull As Double 'If needed instead.
Dim dRandNumDisplay As Double 'If needed instead.




Rate If you store a random number and then display the number it, when displaying the random number, w



'The "Rnd" function sometimes returns a value so long that it may be
'automatically converted to scientific-notation before use.  While
'the number itself will still be less than 1, it may "appear" to be
'greater than 1 by looks only (especially if you want to only use
'the first few digits of that random number for display purposes).
'The following sub-routine gets around this problem by first grabbing
'the left 6 digits of the random number and storing it.  By grabbing
'only the left 6 digits of the original random number, we are in essence
'creating a new number of that length.  That new number is too short to
'be considered for scientific notation by the computer.  Now, if the
'newly stored number is greater or equal to one, we just divide the
'number by 10 to move the decimal to the left one position.  For cleanup,
'we then convert the number down to length we want for our display purposes.
'This will finally give us a rough displayable version of the random number
'that was created.  You can also always first store the full random number
'in a different variable, and then store the left 6 digits of that variable
'for editing for display purposes.  That way, you do not lose the any
'accuracy you might need from having the original full random number--you
'would use the full one for your calculations, and the shortened one for
'display purposes only.
Private Sub cmdNewRand_Click()

    'Store the left six digits of a NEW random number:
    'We are only storing the left six, as we want to
    'convert it from scientific-notation if needed.
    dRandNum = Left(Rnd, 6)

    'Alternatively, we could have stored the full random number
    'in a separate variable, and then store the left six of that
    'variable for the following code.
    'Example:
    'dRandNumFull = Rnd
    'dRandNumDisplay = Left(dRandNumFull, 6)
    '... If you take the left 6 digits of "Rnd" again, you
    'would then end up getting a NEW, DIFFERENT random number
    'for display purposes, which defeats the purpose of this.

    'If the stored random number is greater than or equal to one
    '(due to scientific-notation), then move the decimal
    'to the left one spot:
    If (dRandNum >= 1) Then

        'This will move the decimal left one spot.
        dRandNum = dRandNum / 10

    End If  'End If-Statement to move the decimal left one spot.
    
    'Now, convert the our random number to the actual maximum length we want:
    dRandNum = Left(dRandNum, 5)

    'Display the stored random number:
    txtRandNum.Text = dRandNum

End Sub     'End New_Rand Sub.




Download this snippet    Add to My Saved Code

If you store a random number and then display the number it, when displaying the random number, w Comments

No comments have been posted about If you store a random number and then display the number it, when displaying the random number, w. Why not be the first to post a comment about If you store a random number and then display the number it, when displaying the random number, w.

Post your comment

Subject:
Message:
0/1000 characters