# How do I generate a level randomly?

I am currently hard coding 10 different instances like the code below, but but I'd like to create many more. Instead of having the same layout for the new level, I was wondering if there is anyway to generate a random X value for each block (this will be how far into the level it is). A level 100,000 pixels wide would be good enough but if anyone knows a system to make the level go on and on, I'd like to know that too. This is basically how I define a block now (with irrelevant code removed):

block = new Block(R.drawable.block, 400, platformheight);
block2 = new Block(R.drawable.block, 600, platformheight);
block3 = new Block(R.drawable.block, 750, platformheight);


The 400 is the X position, which I'd like to place randomly through the level, the platformheight variable defines the Y position which I don't want to change.

You can have a list called "levels". You can also have a function for creating a level, ie:

var Level = function (x) //x is the number of blocks
{
var self = this;

self.x = x
self.blocks = [];

for (var i = 0; i < self.x; i ++)
self.blocks.push(new Block(R.drawable.block, 100000*Math.random(), platformheight);)
}

var levels = [];

for (i = 0; i < n; i ++)
levels.push(new Level(Math.round(Math.random()*m)))//m being the number of blocks you would like to have most in a level


Now, what does this do?

1. Makes a function for instancing levels
2. Makes an array for storing levels
3. Fills that array with n levels

The process of creating a Level:

1. new Level(n) get called
2. The level now knows that it has n blocks, stores that info in his self.x
3. The level actually makes n (or self.x) blocks, putting them all in an array called self.blocks

Now, Math.random() gives you a random number between 0 and 1. 0.73, for example. If you multiply that by 100 000, the said width of levels, you get a random x position in that level (73 000, for example).

When you do Math.round(Math.random()*x), you get a whole number between 0 and x, including x.

For example:

Math.round(Math.random()*10) could give you 0, 10, 4, 7, and so on.

If you have any questions, or if you guys notice I'm wrong about something, notify me in the comments!

• God damn it, wrong language! I thought it was Javascript... You can still get it, though, the syntax is almost the same, but the object model is not, though. If someone would like to translate my code, that'd be great. – jcora Apr 9 '12 at 21:56
• Hi, my code was java script ... i have found an easier script (i did the question again on stack overflow) if it does not work i will use this script so many thanks... [stackoverflow.com/questions/10080283/… here i have found a good script but i have put a comment to be able to use it.. are you able to answer the comment i put on the answer i got, thanks :) – Charlton Santana Apr 9 '12 at 22:32
• Aha, OK. For the love of god, please don't mix Java and Javascript in the future, they are completely different languages! Anyway, I hope I helped! ;) – jcora Apr 10 '12 at 8:36