Results 1 to 14 of 14
  1. #1
    tzzt123 is offline Member
    Join Date
    May 2013
    Posts
    23
    Rep Power
    0

    Default Issues with generating a random number

    Hello all. I'm trying to figure out how to generate random numbers.

    The code I have written is as follows :

    import acm.program.*;
    import acm.util.*;

    public class SimpleRandom extends ConcoleProgram {
    public void run() {
    //will run shortly

    private RandomGenerator rgen = RandomGenerator.getInstance();


    }
    }


    but I'm getting the following error :

    illegal modifier for parameter rgen. Only final is permitted.


    What am I doing wrong?

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,889
    Rep Power
    8

    Default Re: Issues with generating a random number

    What does it mean for a variable that's inside a method to be private?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    tzzt123 is offline Member
    Join Date
    May 2013
    Posts
    23
    Rep Power
    0

    Default Re: Issues with generating a random number

    It means other methods are not going to be able to see it. I think.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,889
    Rep Power
    8

    Default Re: Issues with generating a random number

    Variables created inside a method are already only visible inside that method. Other methods, and other classes, automatically cannot see the variable. This is called a variable's scope, and it's worth a google to understand what's going on.

    Since that variable is already only visible inside the method, it doesn't make sense to make it private. That's what the compiler is complaining about.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    tzzt123 is offline Member
    Join Date
    May 2013
    Posts
    23
    Rep Power
    0

    Default Re: Issues with generating a random number

    I've just changed it to public, but I am getting the same error. If I remove the public and write nothing, it works. Why?

  6. #6
    tzzt123 is offline Member
    Join Date
    May 2013
    Posts
    23
    Rep Power
    0

    Default Re: Issues with generating a random number

    got it to work ! whoo hoooooooo!!!!!!!!!!!!

    thanks !!!

  7. #7
    tzzt123 is offline Member
    Join Date
    May 2013
    Posts
    23
    Rep Power
    0

    Default Re: Issues with generating a random number

    oops.

    I mean.

    This is the code now:

    import acm.program.*;
    import acm.util.*;

    public class DiceTest extends ConsoleProgram {
    public void run() {
    //will run shortly

    RandomGenerator rgen = RandomGenerator.getInstance();
    rgen.nextInt(1,6);
    println(rgen);
    }
    }


    and this the outcome:

    acm.util.RandomGenerator@1764bel


    WHAT ?

  8. #8
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,862
    Rep Power
    19

    Default Re: Issues with generating a random number

    You;re trying to print the generator.
    I presume you actually want to print the random number, which is the value returned by nextInt().
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    tzzt123 is offline Member
    Join Date
    May 2013
    Posts
    23
    Rep Power
    0

    Default Re: Issues with generating a random number

    ohh. got it,

  10. #10
    tzzt123 is offline Member
    Join Date
    May 2013
    Posts
    23
    Rep Power
    0

    Default Re: Issues with generating a random number

    ok. So what I tried to do is create a program that throws 2dice based on a random number between 1 and 6 and tell me how many times it had to throw the dice before it reached a total of 12

    import acm.program.*;
    import acm.util.*;

    public class DiceTest extends ConsoleProgram {
    public void run() {
    //will run shortly
    int total = 0;
    int counter = 0;

    while (total != 1) {
    total = Addup();
    counter = counter++;


    }
    println("I threw the dice " + counter + " times to get 12");
    }


    public int Addup() {
    int x = ThrowDice();
    int y = ThrowDice();
    int total = x+y;
    return total;

    }
    public int ThrowDice() {
    RandomGenerator rgen = RandomGenerator.getInstance();
    int x = rgen.nextInt(1, 6);
    return x;

    }
    }

    buttttttttttttttt i get errors !

  11. #11
    tzzt123 is offline Member
    Join Date
    May 2013
    Posts
    23
    Rep Power
    0

    Default Re: Issues with generating a random number

    fixed.

    import acm.program.*;
    import acm.util.*;

    public class DiceTest extends ConsoleProgram {
    public void run() {
    //will run shortly
    int total;
    int counter = 0;
    total = Addup();
    while (total != 12) {
    total = Addup();
    counter = counter+1;


    }
    println("I threw the dice " + counter + " times to get 12");
    }


    public int Addup() {
    int x = ThrowDice();
    int y = ThrowDice();
    int total = x+y;
    return total;

    }
    public int ThrowDice() {
    RandomGenerator rgen = RandomGenerator.getInstance();
    int result = rgen.nextInt(1, 6);
    return result;

    }
    }

  12. #12
    tzzt123 is offline Member
    Join Date
    May 2013
    Posts
    23
    Rep Power
    0

    Default Re: Issues with generating a random number

    Any ideas how I can improve it ?

  13. #13
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,889
    Rep Power
    8

    Default Re: Issues with generating a random number

    Quote Originally Posted by tzzt123 View Post
    Any ideas how I can improve it ?
    By using the code tags to preserve formatting.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  14. #14
    tzzt123 is offline Member
    Join Date
    May 2013
    Posts
    23
    Rep Power
    0

Similar Threads

  1. generating random integers
    By javaFREEK in forum New To Java
    Replies: 7
    Last Post: 10-19-2012, 09:56 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 double random number between 0 and 1
    By sara12345 in forum New To Java
    Replies: 6
    Last Post: 04-17-2010, 08:04 PM
  4. generating random number between 0 and 1
    By sara12345 in forum New To Java
    Replies: 1
    Last Post: 04-17-2010, 07:54 PM
  5. Generating a random number
    By oridov in forum New To Java
    Replies: 2
    Last Post: 11-29-2008, 05:12 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
  •