Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Math.Random()

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Location
    Texas.
    Posts
    12
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Math.Random()

    I get the fact that it's an easy concept. The lab my teacher wanted us to construct is a random phone number generator.
    The area code has to be 218, 312, or 731, it can't be anything else. He told us that we can use Math.random() for this method, but I cannot think of a way how the computer can generate those numbers unless there's an if statement. Any help? I'll post my code if needed. I just don't know the concept I am supposed to use.


  2. #2
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Math.Random()

    can you explain the three options: 218, 312, 731. Do you want the program to randomly pick one of these 3 and then add 8 more random digits to create a 11 digit number?

  3. #3
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Math.Random()

    to use the Math.Random() class firstly:

    import the Random class:
    import java.util.Random;

    then in your main class section to create a Random object:
    Random rand = new Random();

    then to create for example 11 random digits ... :

    digit1 = rand.nextInt(10);
    digit2 = rand.nextInt(10);
    digit3 = rand.nextInt(10);
    digit4 = rand.nextInt(10);
    digit5 = rand.nextInt(10);
    digit6 = rand.nextInt(10);
    digit7 = rand.nextInt(10);
    digit8 = rand.nextInt(10);
    digit9 = rand.nextInt(10);
    digit10 = rand.nextInt(10);
    digit11 = rand.nextInt(10);


    rand.nextInt(10) generates a random number between 0 - 9 inclusive .

    This should help you.


    Get back if you've any more questions.
    Last edited by djl1990; October 26th, 2011 at 06:44 PM.

  4. #4
    Junior Member
    Join Date
    Oct 2011
    Location
    Texas.
    Posts
    12
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Math.Random()

    First off, he wanted the area code to be only these, but random each time: 218, 312, 731.
    The next three numbers needed like a range from 231 to 750. So I set up a variable
    Then the last four numbers were to be from range 0-9999 (except for 1212).
    So I set up an if statement.
    The gist of the code looks like this, I left my file on my school computer.
    int areacode;
    areacode = //problem here, IDK what to put
    int middlethree;
    middlethree =(int)(231 + 519 * Math.random());
    int lastfour;
    lastfour = (int)(9999 * Math.random());
    if (lastfournumbers != 1212) {
    System.out.printf("%3d, %3d, %3d", areacode + middlethree + lastfournumbers);
    }

  5. #5
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Math.Random()

    Quote Originally Posted by xionyus View Post
    I get the fact that it's an easy concept. The lab my teacher wanted us to construct is a random phone number generator.
    The area code has to be 218, 312, or 731, it can't be anything else. He told us that we can use Math.random() for this method, but I cannot think of a way how the computer can generate those numbers unless there's an if statement. Any help? I'll post my code if needed. I just don't know the concept I am supposed to use.
    First, create your Random object
    Random gen = new Random();
    You have three options, and you need to randomly choose one.
    int areaCodeType = gen.nextInt(3);
    Then, based on that randomly generated number, choose your area code
    int areaCode = -1;
    switch(areaCodeType)
    {
      case 0:
        areaCode = 218;
        break;
      case 1:
        areaCode = 312;
        break;
      case 2:
        areaCode = 731;
        break;
    }

Similar Threads

  1. Generation of random number using random class
    By JavaPF in forum Java SE API Tutorials
    Replies: 1
    Last Post: December 7th, 2011, 05:46 PM
  2. Creating a new class and dont know how to generate a random number math code
    By beatlebaby70 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 15th, 2011, 03:03 PM
  3. Confusion with Math.toDegrees() and Math.toRadians(). Please help.
    By marksquall in forum Java Theory & Questions
    Replies: 3
    Last Post: June 23rd, 2011, 01:28 AM
  4. Math in Java?
    By [Kyle] in forum Java Theory & Questions
    Replies: 3
    Last Post: September 23rd, 2009, 12:21 PM
  5. Generation of random number using random class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: April 16th, 2009, 06:10 AM