C#2.0中随机分钟 [英] Random minutes in C# 2.0

查看:200
本文介绍了C#2.0中随机分钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

何去有关添加随机分钟,在数据列,这是我的code:

 保护无效btnUpdateTable_Click(对象发件人,EventArgs的发送)
{
    的foreach(在ds.Tables的DataRow博士[0] .Rows)
    {
       ///检查,如果列[退出]为null或空,填充
       如果(dr.IsNull(logout_time))
       {
           ///获取登录柱日期时间
           ///随机日期时间添加到它
           如果(!dr.IsNull(login_time))
           {
               DateTime的DT = Convert.ToDateTime(DR [login_time]);
               DT = dt.AddMinutes; ///&LT(?)? - 在这里我想补充随机分钟
           }
       }
    }

任何帮助非常AP preciated。


  

谢谢大家的帮助下,我在这里的最后code片断:


 的foreach(在ds.Tables博士的DataRow [0] .Rows)
    {
       ///检查,如果列[退出]为null或空,填充
       如果(dr.IsNull(logout_time))
       {
           ///获取登录柱日期时间
           ///随机日期时间添加到它
           如果(!dr.IsNull(login_time))
           {
               DateTime的DT = Convert.ToDateTime(DR [login_time]);
               随机兰特=新的随机();
               //返回random.Next(0,59);
               DT = dt.AddMinutes(rand.Next(0,59));
               DT = dt.AddSeconds(rand.Next(0,59));
               博士[logout_time] = DT;           }       }
    }


解决方案

您可以使用此:

 随机随机=新的随机();的foreach(DataRow的博士...)
{
   INT兰特= random.Next(0,60);
}

作为评论指出的那样,你不需要创建一个新的随机对象每次要创建数字。 (实际上,你可能不应该)。

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;

           }

       }
    }

解决方案

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

这篇关于C#2.0中随机分钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆