C# Random.Next - never returns the upper bound?

前端 未结 6 1994
我寻月下人不归
我寻月下人不归 2020-12-04 15:06
random.Next(0,5)

It never returns the 5 (but sometimes returns the 0.) Why? I thought these are just boundary values that can be returned. Thanks

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-04 15:36

    This has been written a long time ago but I will comment anyway. I think the main reason for that design decision is that most if not all random number generator at their core generate numbers from 0 to 2^32-1. So if you specify Int32.MaxValue you will never get that number. Having an exception for one number must have been not acceptable to the designers so they decided to have the bracket exclusive. Problem solved!

提交回复
热议问题