1

Using System.Random the below always returns 0. I need a random number between 0 and 1. It always returns 0 I would expect some distribution O's and 1's. Thanks

Random random = new Random(); int randomInt = random.Next(0, 1) 
2
  • "I need a random number between 0 and 1" - Then why capture the result in an int ? Commented Dec 8, 2010 at 11:06
  • 1
    I will use the below thanks... mark as answer the answer ;) Commented Dec 8, 2010 at 12:33

5 Answers 5

5

Try

Random random = new Random(); int randomInt = random.Next(0, 2) 

The Max-Value will not be reached...

Sign up to request clarification or add additional context in comments.

Comments

3

See the documentation for this method: http://msdn.microsoft.com/en-us/library/2dx6wyd4.aspx

The number range returned will be from zero to zero. Greater or equal to the first value and less than the second value.

Comments

2

The upper bound is exclusive and won't be reached.

Comments

2

Random.Next will return an int, that's why you always get 0. Use Random.NextDouble instead.

Comments

0

try random.NextDouble(). There is no int between 0 and 1.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.