Page 1 of 1

Java - Tutorial #3 [Random Number Generator]

Posted: Wed May 22, 2013 11:16 am
by DrNayr
Beginner? to code in java you need an IDE to make it easier and I prefer you get Eclipse, the best one so far, compile and run your java projects with a user-friendly GUI.


Objective: Printing a a random number message into the console.

so first of all you need to start Eclipse then start a new Project name it anything you like, then click Next - finish now, right-click on your java project in the explorer, then add new class. for now, name it tutorial.

then open the new class and insert this code:
Code: Select all

import java.util.Random;

class tutorial{
	public static void main(String[] args) {
	Random rand = new Random();
	int number;
	
	for(int counter=1; counter<=10;counter++) {
	number = rand.nextInt(6);
	System.out.println(number + " ");

	}
    
     }

}
Explaination:

First we imported a Random so this works, then we added a new random called "rand" and added an integer called number
then we just did a simple method to get 10 random numbers,below 6 and we just printed them using the println method.

Yup its just that easy :), and you need to play with it, change numbers and stuff , that way you can learn more than tutorial, just play with it :lol:

Re: Java - Tutorial #3 [Random Number Generator]

Posted: Wed May 22, 2013 1:05 pm
by Shim
you can also use a string and two integers to generate random number or text and that doesn't need any namespaces