Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By Junky

Thread: generating random integers

  1. #1
    javaFREEK is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Unhappy generating random integers

    So far in my java programming class i have been doing well, but i have a new assignment that is due this friday october 19th i've been trying to figure it out for many hours now and i am stumped. we are using eclipse IDE. this is my assignment:

    Problem to Solve
    This assignment is to solve problem using loops. Write a program to generate 100 random integers
    in the range of 0 and 99999, and the frequency count of each digit (0, 1, 2, . . . , 9) in these numbers,
    and print a frequency histogram.

    How to Do It:
    The Overall program structure may look like this:
    1. Use ten variables for the frequency counts for the ten digits 0, 1, 2, . . . , 9.
    2. Other variables are also needed. For example, a variable to hold a random number, a variable
    for the digit, etc.
    3. Repeat 100 times (a loop):
    Generate a random number.
    For each digit d in the number, increase the frequency count of d by 1. This is done
    using a loop. Note: we do not know how many digits are in the number.
    4. For each digit from 0 to 9 (a loop):
    Store its frequency count into a variable.
    Display the digit and the frequency count.
    Display the horizontal frequency bar of asterisks using a loop.

    To accomplish the task, Here are some suggestions:
    1. Generate random integers in the range [0, 99999]. Assume the variable is number:
    number = (int)(Math.random() * 99999);
    2. Get the digits of the random number. Since we do not know how many digits in the number,
    you need to use a loop to "peel off" the rightmost digit one at a time using integer division
    (/) and modulus (%) operations.

    i need help on how to begin this program
    if anyone can help me i will be much appreciative of it because i have been very very busy and stressed lately and i need these points for this assignment or at least some points.


    thank you,

    -Alex
    Last edited by javaFREEK; 10-16-2012 at 11:06 PM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: generating random integers

    Quote Originally Posted by javaFREEK View Post
    i need help on how to begin this program
    The same way you start any other program: import statements followed by class header. Actually the best place to start is with a piece of paper and start sketching out some ideas of what variables you will need, what methods you will need and the general flow of the program.
    if anyone can help me i will be much appreciative of it because i have been very very busy and stressed lately and i need these points for this assignment or at least some points.
    People are willing to help but you need to make it easy as possible for us. As you post stands it is simply a "Here's my assignment. Give me the code" type of post.
    If you want our help you need to make an attempt. Write some code. Post it here and include full error messages if you get them. Most importantly of all - ask a specific question. "I don't know how to do me assignment" is not specific and not a question.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: generating random integers

    BTW saying that you are reaaaaaaaally busy is your problem not ours. We all have our own issues/workloads and must manage them. Don't impose your timeframes on us.
    Fubarable likes this.

  4. #4
    javaFREEK is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default Re: generating random integers

    public class project6 {
    private static Scanner stdin;
    public static void main(String[] args) {
    stdin = new Scanner(System.in);

    int number;
    int freq0 = 0, freq1, freq2, freq3, freq4, freq5, freq6, freq7, freq8, freq9;

    number = (int)(Math.random() * 99999);

    switch(number){
    case 1 : System.out.println("*" + freq0);


    }

    }

    }



    }

    }

    this is what i have and i do not know what to do i need the program to generate a random number and count the frequency of each digit then display asterisk of how many times the digit occurs.

  5. #5
    javaFREEK is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default Re: generating random integers

    now i have this

    public class project6 {
    private static Scanner stdin;
    public static void main(String[] args) {
    stdin = new Scanner(System.in);

    int number, digit, min = 0;
    int freq0 = 0, freq1 = 1, freq2 = 2, freq3, freq4, freq5, freq6, freq7, freq8, freq9;

    //display random number.
    number = (int)(Math.random() * 99999);

    System.out.println(number);

    //display digits of random number.



    }

    }

  6. #6
    javaFREEK is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default Re: generating random integers

    how do i display the digits of the number?

  7. #7
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    2

    Default Re: generating random integers

    Quote Originally Posted by javaFREEK View Post
    how do i display the digits of the number?
    System.out.println(number.length);

    :D
    Last edited by Lionlev; 10-18-2012 at 10:37 PM.
    WARNING I am Russian so it's possible that I wont understand you correctly...

  8. #8
    gowthamgutha is offline Member
    Join Date
    Sep 2012
    Location
    Guntur, India
    Posts
    27
    Rep Power
    0

    Default Re: generating random integers

    You can use java.util.Random class's nextInt() method or nextInt(int range);

    See [Blog spam removed] for other data types.
    Last edited by DarrylBurke; 12-31-2012 at 04:12 PM.

Similar Threads

  1. Generating random numbers
    By chinedum in forum New To Java
    Replies: 9
    Last Post: 09-21-2011, 05:03 AM
  2. Math.random generating 0
    By codeAJ in forum New To Java
    Replies: 1
    Last Post: 03-25-2011, 08:17 AM
  3. generating random number between 0 and 1
    By sara12345 in forum New To Java
    Replies: 1
    Last Post: 04-17-2010, 07:54 PM
  4. Generating a random number
    By oridov in forum New To Java
    Replies: 2
    Last Post: 11-29-2008, 05:12 PM
  5. Random Integers
    By www.kwalski.com in forum Java Applets
    Replies: 8
    Last Post: 12-09-2007, 05:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •