CodePaste Logo
New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: C#

Random Dates

50 Views
Copy Code Show/Hide Line Numbers
static void Main(string[] args)
        {
            RandomDateGenerator generator = new RandomDateGenerator(new Random());
            List<DateTime> RandomDates = new List<DateTime>();
        
            for (int i = 0; i < 100; i++)
            {
                RandomDates.Add(generator.RandomDay());
            }
 
            RandomDates.RemoveAll(x => x.Month == 2);
 
            RandomDates.ForEach(x => Console.WriteLine(x.ToLongDateString()));
            Console.WriteLine(RandomDates.Count);
        }
    }
 
    public class RandomDateGenerator
    {
        Random gen;
 
        public RandomDateGenerator(Random gen)
        {
            this.gen = gen;
        }
 
        public DateTime RandomDay()
        {
            DateTime start = new DateTime(1995, 1, 1);
 
            int range = ((TimeSpan)(DateTime.Today - start)).Days;
            return start.AddDays(gen.Next(range));
        }
    }
by jaras
  July 16, 2009 @ 3:39pm

Add a comment


Report Abuse
brought to you by:
West Wind Techologies



If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate