Java - Tutorial #3 [Random Number Generator]

Questions/Tutorials for any programming language not covered in other sections.
2 posts Page 1 of 1
Contributors
User avatar
DrNayr
New Member
New Member
Posts: 14
Joined: Thu May 16, 2013 9:06 am

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:
User avatar
Shim
VIP - Donator
VIP - Donator
Posts: 882
Joined: Wed Dec 14, 2011 5:02 am

you can also use a string and two integers to generate random number or text and that doesn't need any namespaces
Find my programs on Softpedia
2 posts Page 1 of 1
Return to “Misc”