Kilobolt
  • Home
  • Tutorials
    • Game Development Tutorial >
      • Unit 1: Beginning Java >
        • Before you begin...
        • Day 1: Setting Up
        • Day 2: Java Basics
        • Day 3: More Basics
        • Day 4: Java Math
        • Day 5: More Math
        • Day 6: If... else...
        • Day 7: More Control Flow
        • Day 8: Looping
        • Day 9: More on Looping
        • Day 10: Inheritance, Interface
        • Day 11: Threads and Graphics
      • Unit 2: Creating a Game I >
        • Day 1: Foundations
        • Day 2: Basic Framework
        • Day 3: Taking User Input
        • Day 4: Enter the Robot
        • Day 5: Background and Sprites
        • Day 6: Adding Enemies
        • Day 7: Shooting Bullets
        • Day 8: Animations
        • Day 9: 2D-Arrays
        • Day 10: Painting the Tilemap
      • Unit 3: Creating a Game II >
        • Day 1: Level Creation - Part 1
        • Day 2: Level Creation - Part 2
        • Day 3: Level Creation - Part 3
        • Collision Detection Basics
        • Day 4: Collision Detection Part 1
        • Day 5: Collision Detection Part 2
        • Day 6: Collision Detection Part 3
        • Day 7: Health System & Death
        • Day 8: Basic AI & Final Touches
      • Unit 4: Android Game Development >
        • Day 1: Introduction to Android
        • Day 2: Setting up for Development
        • Day 3: Creating our First Android Application
        • Day 4: Parts of an Android Application
        • Day 5: The Android Game Framework: Part I
        • Day 6: The Android Game Framework: Part II
        • Create an Android Game From Scratch (or port your existing game)
        • Day 7: Creating an Android Game (From Start to Finish)
      • Reference Sheet
    • Zombie Bird Tutorial (Flappy Bird Remake) >
      • Unit 1: Building the Game >
        • Introduction
        • Day 1: Flappy Bird - An In-depth Analysis
        • Day 2: Setting up libGDX
        • Day 3: Understanding the libGDX Framework
        • Day 4: GameWorld and GameRenderer and the Orthographic Camera
        • Day 5: The Flight of the Dead - Adding the Bird
        • Day 6: Adding Graphics - Welcome to the Necropolis
        • Day 7: The Grass, the Bird and the Skull Pipe
        • Day 8: Collision Detection and Sound Effects
        • Day 9: Finishing Gameplay and Basic UI
        • Day 10: GameStates and High Score
        • Day 11: Supporting iOS/Android + SplashScreen, Menus and Tweening
        • Day 12: Completed UI & Source Code
    • Android Application Development Tutorial >
      • Unit 1: Writing Basic Android Apps >
        • Before you begin...
        • Day 1: Android 101
        • Day 2: Getting to Know the Android Project
        • Day 3: The Development Machine
        • Day 4: Building a Music App - Part 1: Building Blocks
        • Day 5: Building a Music App - Part 2: Intents
        • Day 6: Building a Music App - Part 3: Activity Lifecycles
  • Forum
  • About Us
    • Contact Us
  • Our Games
    • TUMBL: FallDown
  • Facebook
  • Twitter

September 16th, 2012

2/11/2014

19 Comments

 
Picture
Lesson #1-7: More Fun with Math

First, a quick review:
In Java, you will frequently see statements written like the following:
(Assume that number is an integer) 
Picture

This tends to confuse many beginning programmers, because they are missing the fundamental purpose of the equal sign.

Like I've said before, the equal sign does not just state equality; instead, it is used to ASSIGN a value (hence the name -- assignment operator).

In other words, it doesn't state that the operand on the left and the value on the right are equal. Instead, it says that the operand is now equal to the value - that the operand takes the value on the right side of the equal sign.

Interpreting the example above, you should think to yourself, 'number is now equal to number + 5.' 

This will save you a lot of headache.
If you understand this, here is a list of equivalent expressions that will save you some time in programming: 
Picture
Lesson #1-8: Randomization

Predictability makes for boring games. If you want games to challenge players, you will want to use randomization.
Let's use TUMBL as an example. For those of you who are not familiar with the game, click here.

1. Each row is generated randomly, meaning that the player will not be able to figure out a pattern that lets them stay alive longer.
2. Stars are generated randomly on the left or right side of the screen.
3. Power-ups appear randomly.
And so on.

So how do you integrate randomization to a game?

It's quite simple. Take a look at this example class: 
Picture
In this class named Randomization, we create a Random object called rand.
Let's break up this statement: 
Picture
think of this just like: 
Picture
You are creating a new object called rand using the type Random.

The second part of the statement: 
Picture
assigns a new Random object with a default seed (I will explain seed in a second) as the value of the newly-created rand variable.

In other words...
you are basically telling Java:
"I want to create a Random object called rand. Use the Random class (specifically something called the constructor) to create this new object."

If you are confused, that's okay. We will go into object creation and constructors in a lot more detail in the coming lessons.

Just know that:
Picture
creates a new Random object called rand (you can change this name). 


Mini Lesson : Imports 

When you copy and paste the following class into Eclipse: 

Figure 1: randomization class

// In this class, we will create a Random object called rand.

class Randomization {

   public static void main(String[] args){

      Random rand = new Random();
 
  }

}
You will see red squiggly lines below the word Random, indicating the presence of an error:
Picture
This error occurs because the "Random class" is not recognized by your current class (Randomization class).

Earlier, I mentioned that the src folder contains all your Java code and that the JRE System Library contains importable code that you can incorporate into your own projects.

Think of this Library as one containing "books" (Java classes). When you write an essay and you reference a book, you cite it.

Same with Java. When you use a "book" (Java classes) from the Library, you must state that you are using this "book" so that the compiler knows where to find the Random class and its associated methods.

This is accomplished by importing.
How do we import? It's pretty simple.

1. The easiest way is to press: 
Picture
This will automatically import the class that the compiler thinks you are most likely to use.

2. Another way is to put your mouse over the words that show an error (in this case Random) and click on the quick fix that suggests importing
the Random class from Java.util. 
Picture
Either way, when you successfully import, the error will disappear, and you will see the following above your class name: 
import java.util.Random;
- indicating that your class makes use of this Random class which can be found in the full address of java.util.Random.

The full class, after importing, will look like this.

Figure 2: randomization class with imports

Picture
Well, now that we have a random object called rand, we can use it to randomize things.
The easiest method we will learn is: 
Picture
What does this method do?
When you call this from a random object such as rand like so: 
Picture
It will generate an integer between 0 and 10 (11 numbers).
Using this, you can simulate chance and probability.
How? We will go over probability and if statements tomorrow!

If you have any questions, please post on the Forum!

If you are learning from and enjoying the tutorials, please support Kilobolt Studios!
And Like us on Facebook to receive updates when a new lesson is posted! 

Picture
Go to Day 4: Java Math
Go to Day 6: If... else...
19 Comments
Reece
11/13/2012 11:35:25 am

Good stuff thank you!

I found a small typo when I tried to do the CTRL SHIFT O thing....

Figure 1: randomization class
// In this class, we will create a Random object called rand.

class Randomization {

public static void main(String[] args****there should be a ")" here****{


Thanks much!

Reply
http://www.skintagfree.com/ link
3/19/2013 04:23:02 pm

After maths Java is the hardest for me to understand, Although i read many tutorials and books but i forget.
Hope this would help me.

Reply
Yash
4/2/2013 03:29:14 am

please tell somebody what is the problem...
This is not giving any output....
class Randomization
{
static int sufer = 10;
public static void main(String[] args)
{
r();
}
public static void r()
{
Random rand = new Random();
rand.nextInt(sufer);
System.out.println(sufer);
}




}

Reply
Thiago Ruiz
4/6/2013 10:53:29 am

I think you didn't understand what the .nextInt() method does. Instead of passing your "sufer" variable as an argument you should do:

sufer = ran.nextInt(anotherVariable);

nextInt() is a method wich returns a int, the argument it takes just specify from where it will take the random number, if you pass 10 it will return from 0 to 9, if you pass 20 it will return from 0 to 19. The keyword here is return, you need to assign this return to something.
Use one variable to receive this return and one to use as the argument, if you use the same for both it will work, but is you call nextInt() another time, it will take as argument the random number you just generate

Reply
Louis N
6/4/2013 12:37:37 pm

Instead of assigning a new value to "sufer", he could write the print command as such: System.out.println(rand.nextint(sufer));

Simo
10/15/2013 06:21:42 am

Import the Random class ..

Reply
Albert Lopez
4/2/2013 06:03:50 am

i am having an issue with the lesson #1-8 section.

Here is my code:
class raondomize
{
public static void main(String[] args)
{
Random rand = new Random();
}
}

Here is my error:
Multiple markers at this line
- Random cannot be resolved
to a type
- Random cannot be resolved
to a type

I am become error.

Reply
albert lopez
4/2/2013 04:08:38 pm

I'm dumb. I figured it out.

Reply
Alex
5/25/2013 02:59:29 pm

As somebody else stated there is a missing ) for the main method.

Also the explanation of "the easiest method we will learn" is not clear at all. Providing a bit more detail here and showing the finifhed code will help reader a lot. By reading the comments and doing some Googling I ended up creating an int called randResult and setting it equal to randVar.nextInt(11) as shown below:

import java.util.Random;

class Randomization {

public static void main(String[] args){

Random randVar = new Random();
int randResult = randVar.nextInt(11);
System.out.println(randResult);

}

}

So a random object is created and randVar is set to it. In the next line randResult is set to randVar.nextInt(11) which ends up being a random number between 0 and 10. Isn't this randomizing twice? It created a random object then randomizes it within the confines of 11 results. I suppose this doesn't really matter but it would help my understanding I think. While I was trying to figure it out I got it to output what appeared to be the imported class name followed by @ and a randomized hex value. This can be seen by adding the following line to the above code:

System.out.println(randVar);

Which prints out values like the following:

java.util.Random@5d888759

Reply
James C.
5/26/2013 02:21:31 pm

Alex, sorry that the .nextInt bit was not too clear. I do have a working example in the next lesson though, so I thought it would suffice.

The java.util.Random@5d888759 you are getting is resulting from your attempt to print a Random object.

When you call System.out.println(), the object inside the parentheses is converted to a String. In the case of a Random object, there is no "toString()" method defined, so it will give you the location in memory.

Reply
Alex
5/27/2013 02:04:01 pm

Thanks for the explanation. I saw it when going to the next lesson, but I would think it would be best to have an example here or not mention it until the next tutorial.otherwise it is confusing. I try to fully understand each lesson before moving on the the next. In any case these tutorials are really helpful, thanks!

Elyssa
7/4/2013 03:02:24 am

i followed your code step-by-step and after running the code. it says that "Error: Could not find or load main class Random". What should i do?

Reply
Scarecrow
9/5/2013 04:49:08 pm

I can't get the " rand.next("11") " method right. Here's my code:


import java.util.Random;


class Randomization {

public static void main(String[] args){

Random rand = new Random(11);

int result = rand.next("11");
System.out.println(" 11 ");
}


}


There's a red squiggly line below the word next and it says " The method next(int) in the type Random is not applicable for the arguments (String) "
Please help. I'm confused.

Reply
future man
11/11/2013 10:32:50 am

Hey there, short question here

Why must we import Random class from Java.util in order for it to work but when we used System.out.println(); to print our string in earlier lessons, we didn't do any importing from the library?

We don't need to import System.out.println(); ?

Reply
Mario B.
11/17/2013 09:33:46 am

Reply
meds
1/29/2014 09:09:37 pm

It doesn't generate the numbers

Reply
Rina
5/9/2016 10:34:52 am

Hi,
I seem to have a problem with the line:
public static void main(String[] args)
The error is with the "main", any idea why?

Reply
Hardik
8/13/2016 09:29:32 am

Hi james,

First of all i m thanking u for the tutorials....these r awesome and best series for game development....i know that u r not replying any of the comments since 2014....i m posting this in hope of resolution of my issue with random importation....i m not able to import random.util.....i don't know y but i m able to import other like graphics color....i thought that theres a problem with my eclipse or jre so i redownload and install them....but i m not yet able to import random.....if anyone other have solution to this pls reply to this post....i be great thankful to that person.

Thanks

Reply
Saur Sierra
9/8/2017 04:53:56 am

Hi Hardik,

Have you tried Ctrl+shift+O??
It worked form instantly to import Random.

Regards,

Reply



Leave a Reply.

    Author

    James Cho is the lead developer at Kilobolt Studios. He is a college student at Duke University and loves soccer, music, and sharing knowledge.

© 2014 Kilobolt, LLC. All rights reserved.