[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 497
  • Last Modified:

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
0
cainsaw
Asked:
cainsaw
  • 2
1 Solution
 
j2020_usCommented:
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
 
DrWarezzCommented:
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
 
cainsawAuthor Commented:
j2020 us

It works!!!

Thanks
0
 
j2020_usCommented:
All the best with your large program.
Thank you.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now