I have the following problem:
Given is an 8-column grid. Now I want to place an item randomly inside it with a random span-width. I figured it out, how to place it randomly and give it a random width.
But I can’t figure out, how to adjust the width based on parameters.
So for example:
columnStart and columnSpan should be less or equal than 8 but still number 2 should be at least 2.
So columnStart can max only be 6.
Thank you for the help!
I tried it with this:
let columnStart = Math.floor(Math.random() * 8) + 1; let columnSpan = Math.floor(Math.random() * 7) + 2;
Which resulted in objects, starting at columnStart 6 and span over 6 more columns. Which shouldn’t be due it’s only a 8-column grid.
I also tried a do-while loop:
do { let columnStart = Math.floor(Math.random() * 8) + 1; let columnSpan = Math.floor(Math.random() * 7) + 2; } while (columnStart + columnSpan === 8)
But this also didn’t brought the result I tried to achieve
2
Answers