views:

283

answers:

4

Ho do I go about adding random minutes to column in dataset, here is my code:

protected void btnUpdateTable_Click(object sender, EventArgs e)
{ 


    foreach (DataRow dr in ds.Tables[0].Rows)
    {
       ///check if column[logout] is null or empty, fill it
       if(dr.IsNull("logout_time"))
       {
           ///get the login colum datetime
           /// add random datetime to it
           if (!dr.IsNull("login_time"))
           {
               DateTime dt = Convert.ToDateTime(dr["login_time"]);
               dt = dt.AddMinutes(?);/// "?"<--here I want to add random minutes
           }            
       }
    }

Any help greatly appreciated.

Thank you all for the help, here my final Code snippet:

foreach (DataRow dr in ds.Tables[0].Rows)
    {
       ///check if column[logout] is null or empty, fill it
       if(dr.IsNull("logout_time"))
       {
           ///get the login colum datetime
           /// add random datetime to it
           if (!dr.IsNull("login_time"))
           {
               DateTime dt = Convert.ToDateTime(dr["login_time"]);
               Random rand = new Random();
               //return random.Next(0, 59);
               dt = dt.AddMinutes(rand.Next(0,59));
               dt = dt.AddSeconds(rand.Next(0, 59));
               dr["logout_time"] = dt;

           }

       }
    }
+10  A: 

You can use this:

Random random = new Random();

foreach(DataRow dr ...)
{
   int rand = random.Next(0, 60); 
}

As a comment pointed out, you don't need to create a new Random object for every number you wish to create. (Actually, you probably shouldn't).

Kevin
Hmm. Creating a new Random every call? Not a good idea, as under stress it may well end up with the same seed twice. Not so random.
spender
Kevin doesn't specify whether it's inside or outside the `foreach` ...give the man a break!
pianoman
Sure, but worth reinforcing nonetheless
spender
point, should have added more context around my post.
Kevin
Upper bound for `Random.Next` is exclusive, so to get a number from 0 to 59 the call should be `random.Next(0, 60)`
Pavel Minaev
actually shouldn't it be -1 to 60?
Kevin
@Kevin - no, `random.Next(0, 60)` is correct. The lower-bound is inclusive, the upper-bound is exclusive
John Rasch
Sometimes we forget here on Stackoverflow that we are writing snippets to show how something is done. Not production code. Sometimes items are left out for brevity.
David Basarab
David, it's highly likely that person asking the question may use the code in production environment. [Obviously, they don't know the topic well otherwise they wouldn't ask the question] So it's important to note the subtle aspects of the code to educate the OP.
SolutionYogi
Although I agree the post should be complete, I think there is also is some onus on the person asking the question. Once pointed in the right direction that person should look at the code and figure out what it is doing. I can't think of a time ever when I wouldn't review code that I found on a website regardless of where.
Kevin
+7  A: 

Try using Random:

Random randGen = new Random();

foreach (DataRow dr in ds.Tables[0].Rows)
{
   ///check if column[logout] is null or empty, fill it
   if(dr.IsNull("logout_time"))
   {
       ///get the login colum datetime
       /// add random datetime to it
       if (!dr.IsNull("login_time"))
       {
           DateTime dt = Convert.ToDateTime(dr["login_time"]);
           dt = dt.AddMinutes(randGen.Next(0, 60));
           /// "?"<--here I want to add random minutes
       }            
   }
}
pianoman
+1 for better use of Random
spender
Thanks... think it's the same as Kevin's though ಠ_ಠ
pianoman
Upper bound for `Random.Next` is exclusive, so to get a number from 0 to 59 the call should be `random.Next(0, 60)`
Pavel Minaev
Fixed... good catch!
pianoman
+2  A: 

Assuming you don't want your dt object to get moved into the next hour (e.g., you would want any time between 8:00 and 8:59 to get moved up to 8:59 at most), I would suggest making the following changes:

protected void btnUpdateTable_Click(object sender, EventArgs e)
{ 
    Random rand = new Random;

    foreach (DataRow dr in ds.Tables[0].Rows)
    {
       ///check if column[logout] is null or empty, fill it
       if(dr.IsNull("logout_time"))
       {
           ///get the login colum datetime
           /// add random datetime to it
           if (!dr.IsNull("login_time"))
           {
               DateTime dt = Convert.ToDateTime(dr["login_time"]);
               dt = dt.AddMinutes(rand.Next(0, (60 - dt.Minutes)));
           }            
       }
    }
}
Dan Tao
That's a thoughtful addition... I wish the question was a bit more specific!
pianoman
A: 

Also please note that two if-statements within each other can be optimized to:

protected void btnUpdateTable_Click(object sender, EventArgs e) { 
    foreach (DataRow dr in ds.Tables[0].Rows)
       if(dr.IsNull("logout_time") && !dr.IsNull("login_time")) {
          DateTime loginTime = Convert.ToDateTime(dr["login_time"]);
          loginTime = loginTime.AddMinutes(new Random().Next(0,59));
       }
}
Zyphrax
You shouldn't use `new Random()` in a loop like this, or you'll seed it with the same millisecond value continually producing the same number for about 10-20 iterations
John Rasch
An example in C# 3.0: `Enumerable.Range(1, 1000).Select(i => new Random().Next()).ToList().ForEach(Console.WriteLine);`
John Rasch
thanks for the help, did not want to use "and" condition every time
fzshah76