Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET - How to generate random numbers in a range with a certain step size?

I'd like to generate random numbers in a range (say between 0 and 1) - but with a certain step size (say 0.05). There's a python function that does exactly that:

random.randrange ( [start,] stop [,step] )

So my problem is not how to generate random numbers in a range - instead I need a way to do this with a certain step size. How can this be done in .NET?

like image 953
bluebox Avatar asked Jan 20 '26 20:01

bluebox


1 Answers

You could generate a random integer between 0 and 20 and divide the result by 20

Dim rnd = New Random()
Dim nextValue = rnd.Next(21) / 20

This will give you a random number between 0 and 1 (inclusive) in 0.05 increments

like image 149
Patrick McDonald Avatar answered Jan 22 '26 20:01

Patrick McDonald



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!