I have a class i'm going to use for mock databinding to a grid while my team members are still working on the DAL/BLL layers. I was using random to create different mock data within the getters and setters. Everything works great until I add a property that
is of type DateTime. As soon as I add that property all of the data is the same regardless of what I pass into the seed value of random constructor. Any ideas?
i've not tested this; however, it looks like it should work.
g.
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
Thanks. Actually I don't even care if the datetime is random or not. I did at first but really I just want the other data to be random. As soon as I add the datetime property to the class it affects the other properties which contain random in them.
p
private string _firstName = string.Empty;
public string FirstName
{
get
{
Random rnd = new Random();
if (rnd.NextDouble() < 0.5)
{
return "Chris";
}
else
{
return "Jack";
}
}
set
{
this._firstName = value;
}
}
private DateTime _myDate;
public DateTime MyDate
{
get
{
return DateTime.Today;
}
set
{
this._myDate = value;
}
}
I'm not sure how your DateTime property caused this issue, but having a non-random values being created when you actually expect, well, randow values, is a known bad approach of using your Random object.
The Random objects are created using the clock. If you create multiple Random(), chances are, they are going to return the same output when calling Next().
The fix is to use a single Random() object in your application, or in the scope you require, and just call Next() on that same object.
The reason you get the same result every time is because you always return DateTime.Today. You could do it like you did for FirstName, generate a random number and choose one of two dates based on the number generated.
But ... this seems like a crazy difficult way to design time data. Just hardcode a bunch of information and then bind the collection to the data source for the grid. Just type it in, don't mess around with random and getters and setters.
ny2244111
Member
184 Points
34 Posts
Random property and datetime
May 08, 2012 04:32 PM|LINK
I have a class i'm going to use for mock databinding to a grid while my team members are still working on the DAL/BLL layers. I was using random to create different mock data within the getters and setters. Everything works great until I add a property that is of type DateTime. As soon as I add that property all of the data is the same regardless of what I pass into the seed value of random constructor. Any ideas?
gerrylowry
All-Star
20515 Points
5713 Posts
Re: Random property and datetime
May 08, 2012 04:40 PM|LINK
@ ny2244111
http://www.alexandre-gomes.com/?p=128 "How To: Random DateTime(s)"
i've not tested this; however, it looks like it should work.
g.
ny2244111
Member
184 Points
34 Posts
Re: Random property and datetime
May 08, 2012 05:53 PM|LINK
Thanks. Actually I don't even care if the datetime is random or not. I did at first but really I just want the other data to be random. As soon as I add the datetime property to the class it affects the other properties which contain random in them.
p
private string _firstName = string.Empty; public string FirstName { get { Random rnd = new Random(); if (rnd.NextDouble() < 0.5) { return "Chris"; } else { return "Jack"; } } set { this._firstName = value; } }private DateTime _myDate; public DateTime MyDate { get { return DateTime.Today; } set { this._myDate = value; } }bertvanp
Member
15 Points
4 Posts
Re: Random property and datetime
May 08, 2012 07:23 PM|LINK
I'm not sure how your DateTime property caused this issue, but having a non-random values being created when you actually expect, well, randow values, is a known bad approach of using your Random object.
The Random objects are created using the clock. If you create multiple Random(), chances are, they are going to return the same output when calling Next().
The fix is to use a single Random() object in your application, or in the scope you require, and just call Next() on that same object.
Paul Linton
Star
13421 Points
2535 Posts
Re: Random property and datetime
May 08, 2012 07:29 PM|LINK
The reason you get the same result every time is because you always return DateTime.Today. You could do it like you did for FirstName, generate a random number and choose one of two dates based on the number generated.
But ... this seems like a crazy difficult way to design time data. Just hardcode a bunch of information and then bind the collection to the data source for the grid. Just type it in, don't mess around with random and getters and setters.