public class DrawOutTheme
{
public Random Randomizer;
public int Value;
public TextBox TextBox = null; public void RandomValue()
{
Randomizer = new Random();
Value = Randomizer.Next(1, 100);
TextBox.Text = Convert.ToString(Value);
}
}private void button1_Click(object sender, EventArgs e)
{
DrawOutTheme[] DrawOut = new DrawOutTheme[2];
DrawOut[0] = new DrawOutTheme() { TextBox = textBox1 };
DrawOut[1] = new DrawOutTheme() { TextBox = textBox2 };
DrawOut[0].RandomValue();
DrawOut[1].RandomValue();
}当按buttion1时 两个textbox的随机是相同的 怎么按buttion时 是两个textbox的随机数是不相同的 求大神帮助
{
public Random Randomizer;
public int Value;
public TextBox TextBox = null; public void RandomValue()
{
Randomizer = new Random();
Value = Randomizer.Next(1, 100);
TextBox.Text = Convert.ToString(Value);
}
}private void button1_Click(object sender, EventArgs e)
{
DrawOutTheme[] DrawOut = new DrawOutTheme[2];
DrawOut[0] = new DrawOutTheme() { TextBox = textBox1 };
DrawOut[1] = new DrawOutTheme() { TextBox = textBox2 };
DrawOut[0].RandomValue();
DrawOut[1].RandomValue();
}当按buttion1时 两个textbox的随机是相同的 怎么按buttion时 是两个textbox的随机数是不相同的 求大神帮助
解决方案 »
- 《C#入门经典》和《C#本质论》哪本适合初学者自学?
- 分属不同内网的两台电脑如何通信!!
- 怎么样从Microsoft Visual Studio 2008里面得到它里面的那个web设计器
- Reporting Services做报ll表 设置了参数Allow null Value 为啥还说缺少参数
- 问个简单问题,为什么打开一个页面,pageload事件执行了2遍呢?
- 在线等高手帮忙解决网站问题
- 怎样用C#取得 硬盘上存放的一首mp3文件的播放时间长度?
- C#中操作DBF提示“超出系统资源”问题
- Graphics小问题~ :)
- 几个C#小问题, 给分!
- 我想自己做皮肤文件.ssk或者是winform可以用的所有皮肤文件应该怎么做?
- winform , 窗体向包含用户控件中传值……
{
int a = RandomValue();
int b = RandomValue(); while (a == b)
{
b = RandomValue();
} textBox1.Text = Convert.ToString(a);
textBox2.Text = Convert.ToString(b);
} public int RandomValue()
{
return new Random().Next(1, 100);
}
在中间加一句Thread.Sleep(100);但这样还是会出现一样的,只是几率下小一点 DrawOutTheme[] DrawOut = new DrawOutTheme[2];
DrawOut[0] = new DrawOutTheme() { TextBox = textBox1 };
DrawOut[0].RandomValue();
Thread.Sleep(100);
DrawOut[1] = new DrawOutTheme() { TextBox = textBox2 };
DrawOut[1].RandomValue();
{
public Random Randomizer=new Random();
public int Value;
public TextBox TextBox = null;
public void RandomValue()
{
Value = Randomizer.Next(1, 100);
TextBox.Text = Convert.ToString(Value);
}
}private void button1_Click(object sender, EventArgs e)
{
DrawOutTheme[] DrawOut = new DrawOutTheme[2];
DrawOut[0] = new DrawOutTheme() { TextBox = textBox1 };
DrawOut[1] = new DrawOutTheme() { TextBox = textBox2 };
DrawOut[0].RandomValue();
DrawOut[1].RandomValue();
}
将 Randomizer = new Random()这一句话放到 public Random Randomizer=new Random()即可
1、用Random(int)这个构造函数为每个DrawOutTheme实例指定一个不同的值,比如可以利用Guid的随机性。
Randomizer = new Random(Guid.NewGuid().GetHashCode())
2、所有DrawOutTheme实例共享一个Random对象,也就是将Randomizer提升为类的静态成员,然后在类的构造函数里取消对其赋值的操作。
static Randomizer = new Random()
public class DrawOutTheme
{
public Random Randomizer;
public int Value;
public TextBox TextBox = null;
public void RandomValue()
{
Value = Randomizer.Next(1, 100);
TextBox.Text = Convert.ToString(Value);
}
}private void button1_Click(object sender, EventArgs e)
{
Randomizer=new Random();
DrawOutTheme[] DrawOut = new DrawOutTheme[2];
DrawOut[0] = new DrawOutTheme() { TextBox = textBox1 };
DrawOut[1] = new DrawOutTheme() { TextBox = textBox2 };
DrawOut[0].RandomValue();
DrawOut[1].RandomValue();
}