Solved

Random Number Generation in JAVA

Posted on 2004-11-18
421 Views
I am a newbie (to this forum and to programming).  The following is going to be part of a larger program.  I suspect this will be an easy one for the experts in this forum.  I am trying to create a Java program which will generate and print out random numbers.  I can generate a different random number every time I run it, but it repeats the random number (however many times I have specified in the while loop).

import java.io.*;

import java.util.*;

public class RandNumber {

public RandNumber () {}

public static void main (String args [])
{
int RollIt = ScanRoll();

RandNumber Roll = new RandNumber ();

int count = 0;

while (count < 10)
{
Roll.ScanRoll();
System.out.println(RollIt);
count ++;

}//end while loop

}//end of main

public static int ScanRoll()
{
int TheRoll;
TheRoll = 1 + (int)(Math.random() * 40);

return TheRoll;

}//end ScanRoll

}//end of RandNumber

The output:
C:\forte4j\Assignment 3>java RandNumber
37
37
37
37
37
37
37
37
37
37
0
Question by:cainsaw

LVL 1

Accepted Solution

import java.io.*;
import java.util.*;
public class RandNumber {
public RandNumber () {}

public static void main (String args [])
{
//int RollIt = ScanRoll(); Static will not work
RandNumber Roll = new RandNumber ();
int count = 0;
while (count < 10)
{
//Roll.ScanRoll(); May capture in a variable
System.out.println(Roll.ScanRoll());
count ++;
}//end while loop
}//end of main
public int ScanRoll()
{
int TheRoll;
Math.random();
TheRoll = 1 + (int)(Math.random() * 40);
return TheRoll;
}//end ScanRoll
}//end of RandNumber
0

LVL 9

Expert Comment

points to: j2020_us (or, at least, don't award me with any points)..

But you may find this site EXTREMELY useful: www.javaalmanac.com  <-- A very popular, and useful Java Reference.

Good luck with it all,
[r.D]
0

Author Comment

j2020 us

It works!!!

Thanks
0

LVL 1

Expert Comment

All the best with your large program.
Thank you.
0

Join & Write a Comment Already a member? Login.

RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!