The fastest way to generate random logic

So there are several ways to create a random bool in C #:

  • Using Random.Next (): rand.Next(2) == 0
  • Using Random.NextDouble (): rand.NextDouble() > 0.5

Is there any difference? If so, which one actually has the best performance? Or is there another way that I have not seen, maybe even faster?

+70
performance c # random boolean
04 Oct '13 at 21:25
source share
4 answers

The first option is rand.Next(2) executing the following code behind the scenes:

 if (maxValue < 0) { throw new ArgumentOutOfRangeException("maxValue", Environment.GetResourceString("ArgumentOutOfRange_MustBePositive", new object[] { "maxValue" })); } return (int) (this.Sample() * maxValue); 

and for the second option - rand.NextDouble() :

 return this.Sample(); 

Since the first option contains maxValue validation, multiplication, and maxValue , the second option is probably faster .

+61
04 Oct '13 at 21:34
source share

Small gain for option two :

According to MSDN

 public virtual double NextDouble() 

returns

Double precision floating point number greater than or equal to 0.0, and less than 1.0.

So, if you want to evenly distribute the random bool, you should use >= 0.5

 rand.NextDouble() >= 0.5 

Range 1: [0.0 ... 0.5 [
Range 2: [0.5 ... 1.0 [
| Range 1 | = | Range 2 |

+48
Feb 27 '15 at 11:12
source share

I conducted tests with a stopwatch. 100,000 iterations:

 System.Random rnd = new System.Random(); if (rnd.Next(2) == 0) trues++; 

Processors love integers, so the Next (2) method was faster. 3700 vs 7500 ms, which is very significant. Also: I think random numbers can be a bottleneck, I created about 50 of every frame in Unity, even with a tiny scene that noticeably slows down my system, so I also hoped to find a method to create a random boolean. So I also tried

 if (System.DateTime.Now.Millisecond % 2 == 0) trues++; 

but the call to the static function was even slower with 9600 ms. Worth a shot. Finally, I skipped the comparison and created only 100,000 random values ​​to make sure that the comparison of int and double did not affect the elapsed time, but the result was almost the same.

+5
Dec 13 '18 at 12:44
source share

The fastest. Calling the Random.Next method has less overhead. The extension method below works 20% faster than Random.NextDouble() > 0.5 , and 35% faster than Random.Next(2) == 0 .

 public static bool NextBoolean(this Random random) { return random.Next() > (Int32.MaxValue / 2); // Next() returns an int in the range [0..Int32.MaxValue] } 

Faster than faster. You can generate random booleans using the Random class even faster using tricks. 31 high bits of the generated int can be used during 31 subsequent logical productions. The bellows implementation is 40% faster than previously claimed to be the fastest.

 public class RandomEx : Random { private uint _boolBits; public RandomEx() : base() { } public RandomEx(int seed) : base(seed) { } public bool NextBoolean() { _boolBits >>= 1; if (_boolBits <= 1) _boolBits = (uint)~this.Next(); return (_boolBits & 1) == 0; } } 
0
Jun 26 '19 at 16:11
source share



All Articles