I would like to generate a floating point value in the range of -1 to +1. I used the following snippet, but it's always positive.
Me.Text = (-1 * Rnd() +1).ToString
Printable View
I would like to generate a floating point value in the range of -1 to +1. I used the following snippet, but it's always positive.
Me.Text = (-1 * Rnd() +1).ToString
Rnd is a holdover from VB6 and should not be used in VB.NET. Unfortunately, many tutorials and classes are written by people who haven't learned anything new in a while, including the last 15 years that .NET has been around. Use the Random class and call its Next method. You simply provide a lower bound and upper bound for the range you want the number to be drawn from. That range can be anywhere from Integer.MinValue to Integer.MaxValue.
Since Rnd returns a value between 0 and 1, not including 1, as long as you don't need to hit -1.0 or 1.0 then you should think about it.
-1 to 1 is a range of 2, so generate a number with a range of 0 to 2, i.e. 2*Rnd() and subtract 1.
Of course since you're using .Net you really should be using the Random Class instead of Rnd.