0

Hi,
I'm trying to convert random numbers between 0.1 and 1. Here is what I have done.

Dim piped As Double
Randomize()
piped = CDbl(CDbl((1.0 * Rnd()) + 0.0))
If (piped < 1) Then
pipediameter.Text = "0" & Str(piped)
Else
pipediameter.Text = Str(piped)
End If

But I want the program to show random values between 0.1 and 1.0. In the randomization, the random values must be in multiples of 0.1. E.g. 0.1, 0.2, 0.3 0.4.

Can someone please help

4
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by nji2rhyme
0

Why not just create a random integer between 1 and 9, throw it into a string and then add a "0." in front of it?
That would give you the 0.1, 0.2 and so on, as a String.
Once that is done, you can convert the string into a double.

Dim random As New Random()
Dim number As String = random.Next(1, 9).ToString()
number = "0." & number
Dim result As Double = Double.Parse(number)
0

Why not just create a random integer between 1 and 9, throw it into a string and then add a "0." in front of it?
That would give you the 0.1, 0.2 and so on, as a String.
Once that is done, you can convert the string into a double.

Dim random As New Random()
Dim number As String = random.Next(1, 9).ToString()
number = "0." & number
Dim result As Double = Double.Parse(number)

Divide by 10 and avoid string concatenation and converting.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.