C# 2.0 中的随机分钟

发布于 2024-07-27 12:58:35 字数 1352 浏览 7 评论 0原文

我如何向数据集中的列添加随机分钟,这是我的代码:

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
           }            
       }
    }

非常感谢任何帮助。

感谢大家的帮助,这是我的最终代码片段:

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;

           }

       }
    }

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;

           }

       }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

国产ˉ祖宗 2024-08-03 12:58:36

尝试使用随机

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
       }            
   }
}

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
       }            
   }
}
眉黛浅 2024-08-03 12:58:36

假设您不希望 dt 对象移至下一小时(例如,您希望 8:00 到 8:59 之间的任何时间最多移至 8:59),我建议您进行以下操作变化:

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)));
           }            
       }
    }
}

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)));
           }            
       }
    }
}
残花月 2024-08-03 12:58:36

另请注意,彼此内的两个 if 语句可以优化为:

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));
       }
}

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));
       }
}
猫烠⑼条掵仅有一顆心 2024-08-03 12:58:35

您可以使用它:

Random random = new Random();

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

正如评论指出的那样,您不需要为您希望创建的每个数字创建一个新的 Random 对象。 (实际上,你可能不应该)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文