cainsaw
asked on
Random Number Generation in JAVA
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
j2020 us
It works!!!
Thanks
It works!!!
Thanks
All the best with your large program.
Thank you.
Thank you.
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]