我想要做的是檢索隨機的「x」和「y」值來將它們分配給我的每個陷阱。我已經學會了如何獲得特定範圍內的隨機數,但我嘗試過各種方法來檢索第一個隨機xvalue和yvalue來分配它們,但我收到「System.Random」。分配隨機數字c#
編輯:謝謝大家的答案和幫助,但我仍然不清楚我如何獲得隨機值並分配它們。我試圖做的是,如果我把3作爲陷阱的數量,程序會給出6個值,3對於x座標,3對於y,我有什麼麻煩計算出我是如何得到它們的。例如。 x的RNG值是(4,7,1),y值是(8,1,6)。即時嘗試使它陷阱1將有值(4,8)分配給它trap2(7,1)和trap3(1,6)。任何指導或提示都會有所幫助。
Console.WriteLine ("Please enter the number of traps.");
Random rndX = new Random();
Random rndY = new Random();
int traps;
traps = Convert.ToInt32 (Console.ReadLine());
Console.WriteLine ("X coordinates for the traps", traps);
for (int X = 1; X <= traps; X++)
{
Console.Write ("{0}", rndX.Next (1, 11));
Console.WriteLine();
}
Console.WriteLine ("Y coordinates for the traps");
for (int Y = 1; Y <= traps; Y++)
{
Console.Write ("{0}", rndY.Next (1, 11));
Console.WriteLine();
}
我一直試圖獲取隨機值
Console.WriteLine ("{0,0}",rndX, rndY);
謝謝你看在這個:d 任何提示或引導到我應該怎麼做,這將是極大的讚賞
INT N = rndX。接下來(); int n = rndY .Next(); –
只是改變了我的答案來匹配它,只需使用可接受值的數組,選擇一個隨機小於數組大小的整數,並抓取相應的元素。見下文。 – evolvedmicrobe