Home All Groups Group Topic Archive Search About

Why Randomize() won't work?

Author
29 Jan 2006 10:03 PM
di98mase
This code found at the Reference page of Randomize() at MSDN won't run
om my installation
' Initialize the random-number generator.
Randomize()
' Generate random value between 1 and 6.
Dim value As Integer = CInt(Int((6 * Rnd()) + 1))

Why?

The compiler don't like the line "Randomize()"?

Do I have to add some module or something? How do I know that that I
have VBA.Math in my include paths?

Regards,

Sebastian "Newbee" in VBA

Author
29 Jan 2006 10:24 PM
Rick Rothstein [MVP - Visual Basic]
> This code found at the Reference page of Randomize() at MSDN won't run
> om my installation
> ' Initialize the random-number generator.
> Randomize()
> ' Generate random value between 1 and 6.
> Dim value As Integer = CInt(Int((6 * Rnd()) + 1))
>
> Why?

You can't declare a variable's type and assign it a value in a single
statement. Try this and see if it works for you...

Dim value As Integer
Randomize
value = CInt(Int((6 * Rnd()) + 1))

I don't program in VBA, so I am not completely familiar with how it works;
but the Randomize statement should only be run one time per running of your
application. In the compiled VB that I work in, I would put the Randomize
statement in the form's Load event and use the other parts of the code where
required.

Rick
Author
30 Jan 2006 9:01 AM
di98mase
Hi Rick,

You are right, I also noticed that I couldn't declare an assign a
variable at the same time but my concern was that the call
"Randomize()" didn't compile. You wrote  "Randomize" without any
paranthesies is that how it should be done? Anyway, I am confused why
the code from MSDN doesn't compile??

I'll try to just write Randomize instead of Randomize().

Thanks

/Sebastian
Author
30 Jan 2006 9:45 AM
Mike Williams
"di98mase" <di98m***@hotmail.com> wrote in message
news:1138608596.029122.15530@g14g2000cwa.googlegroups.com...

> I'll try to just write Randomize instead of Randomize().

You haven't posted a link to the Microsoft code you mention, but perhaps
whatever it is was implying that you can if you wish follow Randomize with a
number, as in Randomize(some value). VB uses that number to "seed" the
randon number generator, and if you omit the number (and also omit the
parentheses) then VB will use the value returned by the system timer.
Normally you would use Randomize just once at the start of your code
(otherwise if you repeatedly use it you will upset the "randomness" of the
generator). Using a number in parenthesis with Randomize is useful because
you can use it to "seed" the random number generator to a specific point (so
that it repeats the same sequence of random numbers every time). To do that
you use the *Rnd* function with a negative argument immediately followed by
Randomize (the same value as before).

Mike
Author
30 Jan 2006 3:20 PM
Michael D. Ober
Based on your original post, the code MSDN sample is for VB.Net.  In VB.Net
(VB 2005), all method and sub functions have the parens.

VB 6
' Initialize the random-number generator.
Randomize
' Generate random value between 1 and 6.
Dim value As Integer
value = Int((6 * Rnd()) + 1)

VB 2005
' Initialize the random-number generator.
Randomize()
' Generate random value between 1 and 6.
Dim value As Integer = Int((6 * Rnd()) + 1)

Mike Ober.


Show quoteHide quote
"di98mase" <di98m***@hotmail.com> wrote in message
news:1138608596.029122.15530@g14g2000cwa.googlegroups.com...
> Hi Rick,
>
> You are right, I also noticed that I couldn't declare an assign a
> variable at the same time but my concern was that the call
> "Randomize()" didn't compile. You wrote  "Randomize" without any
> paranthesies is that how it should be done? Anyway, I am confused why
> the code from MSDN doesn't compile??
>
> I'll try to just write Randomize instead of Randomize().
>
> Thanks
>
> /Sebastian
>
Author
30 Jan 2006 11:03 AM
Larry Lard
di98mase wrote:
> This code found at the Reference page of Randomize() at MSDN won't run
> om my installation
> ' Initialize the random-number generator.
> Randomize()
> ' Generate random value between 1 and 6.
> Dim value As Integer = CInt(Int((6 * Rnd()) + 1))
>
> Why?

This code is VB.NET, not VB6. The giveaway is the second code line -
declaration and assignment in one line is a VB.NET feature.

> The compiler don't like the line "Randomize()"?

Empty parentheses for methods with no parameters are obligatory in
VB.NET (and will be supplied by the IDE if you don't type them), but
not allowed in VB6 (and won't be ignored by the compiler if you do type
them).


I'm not sure if you can still find help for VB6 in the online MSDN.



--
Larry Lard
Replies to group please
Author
30 Jan 2006 11:25 AM
di98mase
Thanks everybody,

I think you helped me out here. So, the issue here is that I am using
VB6 and the online reference manual is for .NET!


/Sebastian
Author
30 Jan 2006 3:30 PM
Ken Halter
"di98mase" <di98m***@hotmail.com> wrote in message
news:1138620317.843434.80710@g49g2000cwa.googlegroups.com...
> Thanks everybody,
>
> I think you helped me out here. So, the issue here is that I am using
> VB6 and the online reference manual is for .NET!
>
>
> /Sebastian

That's enough to drive you nuts... eh? Hunting for VB Classic info on MSDN
is getting similar to hunting for DoDo birds.

--
Ken Halter - MS-MVP-VB - Please keep all discussions in the groups..
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
Freeware 4 color Gradient Frame? http://www.vbsight.com/GradFrameCTL.htm