java定时器获取随机数_定时器和随机数

1.[文件] MainForm.cs ~ 2KB     下载(17)

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

//using example3.RandomHelp;

namespace example3

{

public partial class MainForm : Form

{

Timer timer = new Timer();

int zheng;

int shi;

public MainForm()

{

InitializeComponent();

button1.Click+=button1_Click;

button2.Click+=button2_Click;

// if (textBox3.Text != null)

// {

// string m = textBox3.Text;

}

void timer_Tick(object sender, EventArgs e)

{

//throw new NotImplementedException();

// radioButton2_Click(null,null);

// double r = (example3.RandomHelp.GetIntRandomNumber(int.Parse(textBox1.Text), int.Parse(textBox2.Text)));

// string s = r.ToString();

// label4.Text = s;

if (zheng == 1)

{

int r = (example3.RandomHelp.GetIntRandomNumber(int.Parse(textBox1.Text), int.Parse(textBox2.Text)));

string s = r.ToString();

label4.Text = s;

}

if (shi == 2)

{

double r = (example3.RandomHelp.GetDoubleRandomNumber(int.Parse(textBox1.Text), int.Parse(textBox2.Text)));

string s = r.ToString();

label4.Text = s;

}

}

//整数

private void radioButton1_CheckedChanged(object sender, EventArgs e)

{

RadioButton r = sender as RadioButton;

if (r.Checked == true)

{

zheng = 1;

}

}

//实数

private void radioButton2_CheckedChanged(object sender, EventArgs e)

{

RadioButton r = sender as RadioButton;

if (r.Checked == true)

{

shi = 2;

}

}

//开始

private void button1_Click(object sender, EventArgs e)

{

timer.Interval = int.Parse(textBox3.Text);

//timer.Interval = 500;

timer.Tick += timer_Tick;

timer.Start();

}

//停止

private void button2_Click(object sender, EventArgs e)

{

timer.Stop();

}

}

}

2.[文件] RandomHelp.cs ~ 796B     下载(8)

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

//using System.Windows.Forms.Timer;

namespace example3

{

class RandomHelp

{

public static int GetIntRandomNumber(int min,int max)

{

Random r=new Random();

int ran=r.Next(min, max + 1);

return ran;

}

//很不错的算法

public static double GetDoubleRandomNumber(int min,int max)

{

Random r = new Random();

//很不错的算法

double m=r.NextDouble() * max;

double n = r.NextDouble() * min;

if(m-n>2.0)

return m;

else

return n+3.0;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值