Results 1 to 12 of 12

Thread: [RESOLVED] random object?

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    34

    Resolved [RESOLVED] random object?

    Hi
    I want to use a random object (new random which is available in visual basic) in vba, in a word macro. Is there a way to do it? vba doesn't recognise it, should I use a kind of include/import/declare, i know that it is in mscorlib.dll...
    thanx for your help, as always!

  2. #2
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: random object?

    Is there any reason why you are not using the built-in Rnd Function?
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  3. #3

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    34

    Re: random object?

    I didn't think about it, I need actually a random generator with a initial value (seed), e.g. new Random(222), can I do it with rnd too?
    but I like to know it also because it's a recurrent problem that i need to use classes/objects or functions in VBA which I can use in vb.
    Is it possible to use all classes available in VB in VBA somehow?

  4. #4

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    34

    Re: random object?

    I just tested rnd() can't generate a "seeded" random number.

  5. #5
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: random object?

    Yes you can, but you need to use the Randomize statement to "seed" the RND statement.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  6. #6

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    34

    Re: random object?

    well, it's not the same result when i run this macro twice:
    VB Code:
    1. Sub test()
    2.  
    3. Randomize (222)
    4. MsgBox Rnd
    5. MsgBox Rnd
    6.  
    7. End Sub
    normally it is the same result when I execute the same example in a vb application with the Random() class as many times I want.

  7. #7
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: random object?

    From the VBA help file on Randomize
    Note To repeat sequences of random numbers, call Rnd with a negative argument immediately before using Randomize with a numeric argument. Using Randomize with the same value for number does not repeat the previous sequence.
    Based on that, I changed you code to the following
    VB Code:
    1. Sub test()
    2.  
    3.     Rnd (-1)
    4.     Randomize (222)
    5.     Debug.Print Rnd
    6.    
    7.     Rnd (-1)
    8.     Randomize (222)
    9.     Debug.Print Rnd
    10.  
    11. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  8. #8

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    34

    Re: random object?

    great Declan! works perfect... Thank you

    being curious I'd like to know however if I can use the random() class too, like I said a vb class in a vba macro.???

  9. #9
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: random object?

    The short answer is "yes,sometimes".
    If you can add a reference to the object library that you can use the class, if you can't add the reference you can't use the class.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  10. #10

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    34

    Re: random object?

    thanx for rapid replies, it help really.
    how can I add a reference? I know to use fuctions or subs I use the "declare" instruction, but to use a classe?

  11. #11
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: random object?

    Under the Tools/References menu, you will see a list of avaialable libraries.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  12. #12

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    34

    Re: random object?

    thanx a lot Declan, it was a great help!

Posting Permissions

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



Click Here to Expand Forum to Full Width