Rnd function to hh:mm:ss - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Rnd function to hh:mm:ss

johnl12johnl12 Posts: 1Member

I'm wanting to add +- 30 seconds to a field within my code.

I've got it somewhat working, but it seems to add a random number outside the ranges of the ones I've set.

Below is the code I'm using:
' Set random number generator limits
upperBound = (0.000347001 * 2) * 1000 ' 60 seconds
lowerBound = 0 ' 0 seconds

' Loop around
For lp = 0 To j - 2

rnd_adjustment = (upperBound - lowerBound + 1) * Rnd + lowerBound
rnd_adjustment = rnd_adjustment / 1000

If (rnd_adjustment > 0.000347001) Then
rnd_adjustment = -rnd_adjustment / 2
End If

My thinking behind the code is as follows:

1. 0.000347001 is equal to 30 seconds when converted to hh:mm:ss format.
2. To use the Rnd function I decided to create a number between 0 and (0.000347001*2) and then if above 0.000347001 divice by 2 and turn negative.

The output of my code does change the original hh:mm:ss but the changes are not limited to +-30 seconds as intended.

Could anyone shed light on this? Is it my method or is it something to do with the datatype (as it 0.000347001 it needs to be a Double?)
Any help sorting this out would be hugely appreciated.



Sign In or Register to comment.