C
C#5mo 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
Angius5mo ago
Does the loop only run once? Or do you only get one box added?
Vegas
VegasOP5mo ago
1 boz gets added box
Vegas
VegasOP5mo 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
VegasOP5mo ago
1
Angius
Angius5mo ago
Use the debugger to see what's going on Maybe amount is always 1?
Vegas
VegasOP5mo ago
[DOTNET] 5 [DOTNET] 5 [DOTNET] 5 [DOTNET] 5 [DOTNET] 5
Angius
Angius5mo ago
?
Vegas
VegasOP5mo 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