Int(1000000000000000000 * Rnd + 9000000000000000000) = Int(1E+18 * Rnd + 9E+18)BLAH!-VBForums
Results 1 to 15 of 15

Thread: Int(1000000000000000000 * Rnd + 9000000000000000000) = Int(1E+18 * Rnd + 9E+18)BLAH!

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2001
    Posts
    154

    Int(1000000000000000000 * Rnd + 9000000000000000000) = Int(1E+18 * Rnd + 9E+18)BLAH!

    Okay, I would like to be able to do the first one i wrote down, but everytime i type it in, visual basic converts it, which messes up what the number is supposed to be. Can somebody tell me how I can force visual basic to keep the FIRST Int function? Thanks

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Mar 2001
    Posts
    154

    For Example....

    the SECOND int function returns this: 9.44011932611465E+17

    When it should (since the int function is used) return a WHOLE number such as 14, 584949, 45949, etc.

  3. #3
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    What you trying to do huh???

    You want a random number between 1000000000000000000 and 9000000000000000000 ?

  4. #4
    Tygur
    Guest
    It does return a whole number.
    9.44011932611465E+17 is the same as 944011932611465000

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Mar 2001
    Posts
    154
    A random number between:

    9000000000000000000 and 9999999999999999999

    using the way of:

    (highnumber - lownumber + 1) * Rnd + lownumber

  6. #6
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    It is a wee bit more tricky than that Tygur

  7. #7
    Tygur
    Guest
    It is a wee bit more tricky than that Tygur
    How do you figure?

  8. #8
    Tygur
    Guest
    9.44011932611465E+17 is scientific notation. I'm sure they got other names for it, too.

    Basically, you take what's before the E and multiply it by ten to the power of what's after the E (same as multiplying it by 1 with that many zeros after or moving the decimal point over to the right that many places).

    If anyone's got a better explanation, go ahead..

  9. #9
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    Tygur, try and get a function to produce a random number between

    9000000000000000000 and 9999999999999999999...

    See if it is ever possible to get 1... you'll see what I mean.

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Mar 2001
    Posts
    154
    I know what scientific notation is and everything, i passed elementary yesterday. I think what you are trying to say is it isnt possible for visual basic to generate numbers that long, so it uses scientific notation to represent them. I guess I will have to split up this number into 3 or 4 different Int functions, right?

  11. #11
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    The problem is this.

    When you get a random number with that formula you bascially get a number between 0 and 1 from the rnd function.

    Lets take 0.044 for example.

    If I want a number between 0 and 100 I just have to multiply rnd by 100 and then just get the integer portion and then add 1. This works because rnd generates number less than 1/100th.

    So you see to get a number between 0 and 1000000000000000000, means your rnd function must return a number between 0.0000000000... well you get my drift you need a very small number indeed. Rnd does not generate sufficiently small enough numbers to do achieve this.

    So unfortunately you cannot use rnd to generate your random number when working with such large numbers.

    This is the problem to overcome.

  12. #12
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    Here is an attempt. Perhaps I have introduced some bias?

    Code:
    Private Sub Command1_Click()
     MsgBox Len(Random("9000000000000000000", "9999999999999999999"))
    End Sub
    Code:
    Private Function Random(ByVal Min$, ByVal Max$) As Variant
     Dim x, y, z
     Randomize
     x = CDec(Min): y = CDec(Max): z = CDec(Rnd) * CDec(Rnd) * CDec(Rnd)
     Random = CDec(((y - x + 1) * z + x))
     If InStr(1, Random, ".") Then Random = Left$(Random, InStr(1, Random, ".") - 1)
    End Function

  13. #13
    Tygur
    Guest
    ca1n3:
    When I first saw numbers like that (1E+18 for example), even though I knew what scientific notation was, I didn't know what they meant. It looked like you were in the same situation, and I just explained what scientific notation was, just to be safe.

    Nucleus:
    I know Rnd never returns 1 and it's not precise enough for these big numbers. I just didn't think ca1n3 was worried about that. By the way, your function does seem to introduce bias. It seems to prefer lower numbers.

    Also, you can generate those large numbers without Nucleus's code, but they just won't be precise. They'll just always end in at least some zeros. And they'll have that notation, but CDec can take care of that.

  14. #14
    Hyperactive Member techman2553's Avatar
    Join Date
    Mar 2001
    Location
    <- To your left.
    Posts
    362
    ca1n3,

    Do you need a random number with 19 digits, or do you need a set of random digits 19 wide ??? There is a big difference.

    If you actually need a truly random number with 19 digits, then you're going to have a problem doing it in VB. VB will only keep track of 16 digits, period. They can be padded with zero on either end, but there will only be up to 16 non-zero digits, no matter what combination of functions you use.

    If you only need a set of 19 random digits, then this can be easily done by concatenating 19 random digits (0-9) in a string. The problem with this is that there won't be an even distribution of possible numbers. Think about the probability of getting a number less then 1000 out of a 19 digit random number, versus get 16 consecutive zeros out of random digits (0-9). (0000000000000000999)

    I'm not sure what your needs are, but as long as you try to use your 19 digit number as a number, then you will loose 3 digits to zeros, always.

    To get past seeing the scientific notation thing, try this:

    Debug.Print Format((Rnd) * 1E+18, "###################")

    Good luck !!!
    ----------

  15. #15
    Tygur
    Guest
    VB will only keep track of 16 digits, period. They can be padded with zero on either end, but there will only be up to 16 non-zero digits, no matter what combination of functions you use.
    I have to disagree. The Decimal data type (which can only be stored in a variant) can be more precise than 16 digits.

    Here's an example:
    Code:
    Dim V, V2
    V = CDec("100000000000000000002")
    V2 = CDec("100000000000000000000")
    MsgBox V - V2
    That example shows 2 in a messagebox. And it is keeping track of more than 16 digits.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.