C
C#4mo ago
Vegas

Loop For X times

Im tring to get X amount of boxes to fill depending on random number. Im only getting 1
c#
int count = 0;

Random rdmamt = new Random();
int amount = rdmamt.Next(0,8);
int x = rdmamt.Next(0, 16);
int y = rdmamt.Next(0, 13);

if (count <= amount)
{
for (int i = 0; i < amount; i++)
{
Playarea.Add(new BoxView()
{
Color = Colors.DarkGray,
}, x, y);
}
}
c#
int count = 0;

Random rdmamt = new Random();
int amount = rdmamt.Next(0,8);
int x = rdmamt.Next(0, 16);
int y = rdmamt.Next(0, 13);

if (count <= amount)
{
for (int i = 0; i < amount; i++)
{
Playarea.Add(new BoxView()
{
Color = Colors.DarkGray,
}, x, y);
}
}
Thanks
10 Replies
Angius
Angius4mo ago
Does the loop only run once? Or do you only get one box added?
Vegas
VegasOP4mo ago
1 boz gets added box
Vegas
VegasOP4mo ago
If i goto my station button and back again i only get 1 more box
No description
blueberriesiftheywerecats
try creating Random outside of that method
Vegas
VegasOP4mo ago
1
Angius
Angius4mo ago
Use the debugger to see what's going on Maybe amount is always 1?
Vegas
VegasOP4mo ago
[DOTNET] 5 [DOTNET] 5 [DOTNET] 5 [DOTNET] 5 [DOTNET] 5
Angius
Angius4mo ago
?
Vegas
VegasOP4mo ago
Is it due to only having one Boxview? That time it gave me random number of 5
Shalltear Bloodfallen
x and y are assigned values only once, which is before the loop so even if amount gets assigned to 6 (for example), since x and y are never reassigned in the loop, then, the coordinates of all 6 of the BoxView items will be the same, and will hence overlap on the UI and and therefore appear as only one thats the only thing i can come up with that could be wrong with the code
Want results from more Discord servers?
Add your server