2014-04-16 186 views
0

我想要做的是檢索隨機的「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 任何提示或引導到我應該怎麼做,這將是極大的讚賞

+0

INT N = rndX。接下來(); int n = rndY .Next(); –

+0

只是改變了我的答案來匹配它,只需使用可接受值的數組,選擇一個隨機小於數組大小的整數,並抓取相應的元素。見下文。 – evolvedmicrobe

回答

0

您必須調用Next()方法,就像您在代碼的其他部分所做的那樣。

最後一行代碼在您的類Random類的實例中隱式調用ToString(),該類輸出類類型(在這種情況下爲"System.Random")。

1

這是錯誤的,它調用在隨機類ToString()方法,因此它輸出System.Random

Console.WriteLine ("{0,0}",rndX, rndY); 

,需要準確索引使用{0}{1}用於第一和第二值的值。

Console.WriteLine ("{0}, {1}",rndX.Next(1, 11), rndY.Next(1, 11)); 
0

試試這個

Console.WriteLine ("{0},{1}",rndX.Next(), rndY.Next()); 
0

一旦使用的值被處置,除非您保存它們。

Console.WriteLine ("Please enter the number of traps."); 

Random rnd = new Random(); 
int traps; 

traps = Convert.ToInt32 (Console.ReadLine()); 
Console.WriteLine ("X coordinates for the traps", traps); 

for (int X = 1; X <= traps; X++) 
{ 
    int rx = rnd.Next (1, 11); 
    Console.Write ("{0}", rx); 
    Console.WriteLine(); 
} 

Console.WriteLine ("Y coordinates for the traps"); 
for (int Y = 1; Y <= traps; Y++) 
{ 
    int ry = rnd.Next (1, 11); 
    Console.Write ("{0}", ry); 
    Console.WriteLine(); 
} 

此外,你只需要一個隨機對象。

由於您在循環中檢索值,因此您應該查看單個值的類,並使用List來存儲這些值的集合。

更新:

下面是使用一個類和一個列表的示例:

public class Trap 
{ 
    public int X = 0; 
    public int Y = 0; 
} 

Console.WriteLine("Please enter the number of traps."); 

Random rnd = new Random(); 
int amount; 
List<Trap> traps = new List<Trap>(); 
amount = Convert.ToInt32(Console.ReadLine()); 
Console.WriteLine("Coordinates for {0} traps", amount); 
for(int i = 0; i < amount; i++) 
{ 
    Trap temp = new Trap(); 
    temp.X = rnd.Next(1, 11); 
    temp.Y = rnd.Next(1, 11); 
    Console.WriteLine("Coordinates for Trap {0} - {1},{2}", i + 1, temp.X, temp.Y); 
    traps.Add(temp); 
} 

每個陷阱座標是在列表traps(例如座標1是traps[0]

+0

謝謝你對我的代碼和問題的回答/評論,但我仍然無法做我想做的事。目標是如果我輸入3作爲陷阱數量,並且我收到3個X值和3個Y值,我如何將它們分配或保存到陷阱中。 EG隨機x值(3,7,1),隨機y值(4,6,2)如何從每個值中提取第一個數字並將它們分配給單個陷阱,使陷阱1爲(3,4)陷阱2 (7,6)和陷阱3(1,2)。任何幫助或指導將不勝感激:D – Parti

+0

在我的答案最後一句話討論這一點。 – tinstaafl

+0

@Parti我添加了更多代碼供您查看。 – tinstaafl

2

您需要使用相同的隨機數生成器,C#中的RNG是一個破壞性算法,並且也使用時間戳種子作爲默認值來實現,因此可以很容易地實現:

rndX.Next() 
rndY.Next() 

將返回相同的數字,因爲它們都由於指令太快而被有效地使用相同的時間戳創建。相反,調用相同的RNG兩次,並用數組索引選擇一個值,像這樣:

var arrX= new int[] {1,2,3}; 
var arrY = new int[] { 6,7,8}; 
Console.WriteLine ("{0},{1}",arrX[rndX.Next(3)], arrY[rndX.Next(3))]; 
0

你有沒有嘗試這個代碼:

public string GetRandomNumber(Int32 digit) 
{ 
    if (digit < 0) digit *= -1; 
    if (digit == 0) digit = 1; 
    Random rn = new Random(); 
    string r = ""; 
    for (int i = 1; i <= digit; i++) 
    { 
     r += "9"; 
    } 
    return rn.Next(Convert.ToInt32(r)).ToString(); 
}