|
-
Aug 4th, 2006, 08:54 AM
#1
Thread Starter
Member
[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!
-
Aug 4th, 2006, 09:22 AM
#2
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 
-
Aug 4th, 2006, 09:32 AM
#3
Thread Starter
Member
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?
-
Aug 4th, 2006, 09:42 AM
#4
Thread Starter
Member
Re: random object?
I just tested rnd() can't generate a "seeded" random number.
-
Aug 4th, 2006, 09:46 AM
#5
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 
-
Aug 4th, 2006, 10:06 AM
#6
Thread Starter
Member
Re: random object?
well, it's not the same result when i run this macro twice:
VB Code:
Sub test()
Randomize (222)
MsgBox Rnd
MsgBox Rnd
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.
-
Aug 4th, 2006, 10:12 AM
#7
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:
Sub test()
Rnd (-1)
Randomize (222)
Debug.Print Rnd
Rnd (-1)
Randomize (222)
Debug.Print Rnd
End Sub
Declan
Don't forget to mark your Thread as resolved.
Take a moment to rate posts that you think are helpful 
-
Aug 4th, 2006, 10:17 AM
#8
Thread Starter
Member
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.???
-
Aug 4th, 2006, 10:34 AM
#9
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 
-
Aug 4th, 2006, 10:52 AM
#10
Thread Starter
Member
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?
-
Aug 4th, 2006, 10:58 AM
#11
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 
-
Aug 4th, 2006, 11:05 AM
#12
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|