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

Recommended Answers

All 4 Replies

What the code outputs now? Is it not ok? tell us whats wrong.

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)

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.

Thanks guys. Problem solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.