Blog Home  Home Feed your aggregator (RSS 2.0)  
What did you learn today? - Random.Next
Phil Denoncourt's Technology Rants
 
 Tuesday, October 04, 2005
Random.Next by phildenoncourt
Arg!! Serves me right for not reading the documentation and assuming the way a component works. I have a test framework that I use to generate random data when running unit tests on the data layer. I don't care what the data looks like as long as data is in fact saved. I use Random.Next to get a value that I use for a variety of purposes: Getting a letter, a number, or a boolean value. To get a random boolean value, I was using this code:
System.Random r = new Random();
bool tstResult = r.Next(0,1) ==0 ? true : false;
The problem is that Random.Next will return a value >= the first parameter and < the second parameter. So you can see that my routine always returns true. I had assumed that the arguments were the range of numbers you were interested in.
		
Tuesday, October 04, 2005 3:00:25 PM (GMT Standard Time, UTC+00:00)  #    Comments [0]   DotNet  | 
Copyright © 2010 Phil Denoncourt III. All rights reserved.
DasBlog 'Portal' theme by Johnny Hughes.
Pick a theme: