C# 如何将线程设置为随机生成的数字
我正在尝试创建三个线程。每条线都是一个玩家,有三次击球。快照是从线程中随机生成的数字。我已经试着把这个C# 如何将线程设置为随机生成的数字,c#,multithreading,C#,Multithreading,我正在尝试创建三个线程。每条线都是一个玩家,有三次击球。快照是从线程中随机生成的数字。我已经试着把这个 static int counter = 0; static Thread player1 = new Thread(new ThreadStart(Player1Shot)); static Thread player2 = new Thread(new ThreadStart(Player2Shot)); static Thread pla
static int counter = 0;
static Thread player1 = new Thread(new ThreadStart(Player1Shot));
static Thread player2 = new Thread(new ThreadStart(Player2Shot));
static Thread player3 = new Thread(new ThreadStart(Player3Shot));
static readonly ThreadLocal<Random> random =
new ThreadLocal<Random>(() => new Random(Interlocked.Increment(ref counter)));
static void Main(string[] args)
{
player1.Name = "David";
player2.Name = "James";
player3.Name = "Mike";
player1.Start();
player2.Start();
player3.Start();
//Console.WriteLine("{0}", random.Value.Next());
Console.ReadLine();
}
public static void Player1Shot()
{
try
{
for (int i = 0; i < 3; i++)
{
Console.WriteLine("{0} shot {1}\n", Thread.CurrentThread.Name, random.Value.Next());
}
}
静态整数计数器=0;
静态线程player1=新线程(新线程开始(Player1Shot));
静态线程player2=新线程(新线程开始(Player2Shot));
静态线程player3=新线程(新线程开始(Player3Shot));
静态只读线程本地随机=
新线程本地(()=>新随机(联锁增量(参考计数器));
静态void Main(字符串[]参数)
{
player1.Name=“大卫”;
player2.Name=“詹姆斯”;
player3.Name=“迈克”;
player1.Start();
player2.Start();
player3.Start();
//WriteLine(“{0}”,random.Value.Next());
Console.ReadLine();
}
公共静态void Player1Shot()
{
尝试
{
对于(int i=0;i<3;i++)
{
WriteLine(“{0}shot{1}\n”,Thread.CurrentThread.Name,random.Value.Next());
}
}
< P>但是我希望随机数在0到100之间?这是可能的吗?< /P> < P>不确定你的需求是什么……考虑一些更类似的:
class Program
{
static int counter = 0;
static readonly ThreadLocal<Random> random =
new ThreadLocal<Random>(() => new Random(Interlocked.Increment(ref counter)));
static List<Thread> players = new List<Thread>();
static void Main(string[] args)
{
string[] playerNames = {"David", "James", "Mike" };
foreach(string name in playerNames)
{
Thread T = new Thread(new ThreadStart(PlayerShot));
T.Name = name;
players.Add(T);
}
Console.WriteLine("Waiting for Players to Shoot...\n");
foreach (Thread player in players)
{
player.Start();
}
// wait for all players to be done shooting
foreach (Thread player in players)
{
player.Join();
}
Console.Write("\nPress [Enter] to quit.");
Console.ReadLine();
}
public static void PlayerShot()
{
try
{
for (int i = 0; i < 3; i++)
{
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(random.Value.Next(3, 11))); // 3 to 10 (INCLUSIVE) Second Delay between shots
Console.WriteLine("{0} shot {1}\n", Thread.CurrentThread.Name, random.Value.Next(0, 101)); // 0 to 100 (INCLUSIVE)
}
}
catch (Exception ex) { }
}
}
类程序
{
静态整数计数器=0;
静态只读线程本地随机=
新线程本地(()=>新随机(联锁增量(参考计数器));
静态列表玩家=新列表();
静态void Main(字符串[]参数)
{
string[]playerNames={“大卫”、“詹姆斯”、“迈克”};
foreach(playerNames中的字符串名称)
{
线程T=新线程(新线程开始(PlayerShot));
T.名称=名称;
玩家。添加(T);
}
Console.WriteLine(“等待玩家射击…\n”);
foreach(播放器中的线程播放器)
{
player.Start();
}
//等待所有玩家完成射击
foreach(播放器中的线程播放器)
{
player.Join();
}
控制台。写入(“\n按[Enter]退出。”);
Console.ReadLine();
}
公共静态无效播放者()
{
尝试
{
对于(int i=0;i<3;i++)
{
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(random.Value.Next(3,11));//两次放炮之间的延迟为3到10秒(含3到10秒)
Console.WriteLine(“{0}shot{1}\n”,Thread.CurrentThread.Name,random.Value.Next(0,101));//0到100(包括)
}
}
捕获(例外情况除外){}
}
}
“这可能吗?”我想你已经尝试运行你的代码了,或者?=!结果如何?@MongZhu,可能会返回更大的值(最多)。它会生成一个6位数的数字。我只希望0到100之间的数字使用随机数。下一步(int-minValue,int-maxValue)请参阅: