多个随机数

时间:2017-10-29 22:26:50

标签: c# random

嘿,我怎么能在每辆车上创建col1随机,它可以在某些车辆上相同,但不是所有车辆都可以有相同的颜色。

Random r = new Random();

col1 = r.Next(0, 159);

CreateVehicle(-614, -352, 34,col1, col2, 0);
CreateVehicle(-614, -352, 34,col1, col2, 0);
CreateVehicle(-614, -352, 34,col1, col2, 0);
CreateVehicle(-614, -352, 34,col1, col2, 0);
CreateVehicle(-614, -352, 34,col1, col2, 0);
CreateVehicle(-614, -352, 34,col1, col2, 0);
CreateVehicle(-614, -352, 34,col1, col2, 0);
CreateVehicle(-614, -352, 34,col1, col2, 0);
CreateVehicle(-614, -352, 34,col1, col2, 0);
CreateVehicle(-614, -352, 34,col1, col2, 0);

2 个答案:

答案 0 :(得分:0)

你需要将r.next放在所有这些中,因为你所做的是为col1分配一个值,并在每辆车中分配一个值。

答案 1 :(得分:0)

您需要在创建每辆车之前设置col1 = r.Next(0, 159);。一种简单的方法是将代码放在循环中:

for (int i = 0; i < 10; i++)
{
    col1 = r.Next(0, 159);
    CreateVehicle(-614, -352, 34, col1, col2, 0);
}