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.

Page 1 of 3 123 LastLast
Results 1 to 25 of 65

Thread: Random variant.

  1. #1
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Random variant.

    Hello. I have a program:
    package Dice;
     
    import java.util.Scanner;
     
    public class Dice {
     
    int game;
     
    String [] players;
     
    int [] Wonthenumberofturns;
     
     
    int esk;
     
    int [] er;
     
    int [] turnswinners;
     
    private Scanner scan;
    private Scanner scan2;
    private Scanner scan3;
     
     
    public void Players(){
    scan = new Scanner(System.in);
    System.out.println("Insert how many players play in game:");
    int game = scan.nextInt();
    scan.nextLine();
    players = new String [game];
    Wonthenumberofturns = new int [game];
    for(int i = 0; i < game; i++){
    System.out.println("Insert player number" + Integer.toString(i+1));
    players[i] = scan.nextLine();
    Wonthenumberofturns[i] = 0;
    }
    //scan.close();
    }
     
    public void Turnnumber(){
    scan2 = new Scanner(System.in);
    System.out.println("Enter the desired number of turns");
    game = scan2.nextInt();
    scan2.nextLine();
    er = new int [game];
    turnswinners = new int [game];
    }
     
    public void Go(int Gonumber){
    int Biggest = 0;
    int Record = 0;
    scan3 = new Scanner(System.in);
    for(int i = 0; i < game; i++){
    System.out.println("Enter the number of players rolling the" + players[i]);
    int Rezult = scan3.nextInt();
    scan3.nextLine();
    if (Rezult > Biggest){
    Biggest = Rezult;
    Record = i;
    }
    }
    System.out.println("Turn win the player" + players[Record]);
    turnswinners[Gonumber] = Record;
    er[Gonumber] = Biggest;
    Wonthenumberofturns[Record]++;
    }
     
    public void Winner(){
    for(int i = 0; i < game; i++){
    int winnernumber = turnswinners[i];
    int taskai = er[i];
    System.out.println("Turn" + Integer.toString(i+1) + "won" + players[winnernumber]);
    System.out.println("collected" + Integer.toString(taskai) + "points");
    }
    int winners = 0;
    int winner = 0;
    for (int i = 0; i < game; i++) {
    if (Wonthenumberofturns[i] > winners) {
    winners =Wonthenumberofturns[i];
    winner = i;
    }
    }
    System.out.println("Game winner" + players[winner]);
    }
    public void zaid(){
    Players();
    Turnnumber();
    for(int i = 0; i < game; i++){
    Go(i);
    }
    Winner();
    }
    }
    And how can I change turn number in random?
    I have a code:
    int number = (int)Math.round((Math.random() * 5 + 1));


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Random variant.

    What value does number get when the code is executed?
    Make a small simple test program with that statement in a loop and print out the value of number inside the loop to see what values are created.

    The code you mosted needs formatting. All statements should not start in the first column. Nested statements should be indented.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Random variant.

    Can you correct code? And show me?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Random variant.

    show me
    Here's a sample cut from a program:
            openFileMI.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String htmFile = getFileToRead();
                    if(htmFile == null)
                      return; // nothing chosen
                   //Create an editor pane.
                   loadEditorPane(htmFile);
                  System.out.println("end aP");
                }
            }
            );
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Random variant.

    This cut I can use in my program to make a random turn numbers?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Random variant.

    make a random turn numbers
    See the Random class. It has methods for generating random numbers.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Random variant.

    Like this?:
    public void random() {
    Random dice = new Random();
    int number;
     
    for(int counter = 1; counter<=10;counter++){
    number = dice.nextInt(6);
    	return; 
    }
    }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Random variant.

    Were there any surprises in what was printed? Do you see how to use that method?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Random variant.

    Quote Originally Posted by Lukas View Post
    This cut I can use in my program to make a random turn numbers?
    Wow!

    I am stunned.
    Improving the world one idiot at a time!

  10. #10
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Random variant.

    Quote Originally Posted by Norm View Post
    Were there any surprises in what was printed? Do you see how to use that method?
    I don't know.. I don't understand how to create it...

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Random variant.

    I was talking about the code in post#7 where you were testing using random to see what it would generate.
    What did it print out when it was executed?

    You would need a class and a main() method for testing it.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Random variant.

    I have a main().
    package Dice;
     
    public class Main {
     
     
    /**
     
    *
    @param args
     
    */
     
    public static void main(String[] args) {
     
    Dice game = new Dice ();
    game.zaid();
     
    }
     
    }

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Random variant.

    What about the test program in post #7? Put that in a class with a main() and execute it to see how to use random.
    You wouldn't need to test if you fully understand how to use random. I thought you weren't sure.

    There is something wrong when you copy and paste code here. The posted code has lost its formatting. All statement are shifted left.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Random variant.

    When I start the program, she show me "Select a way to run 'Random' ":
    Java Applet;
    Java application;

    And when I select one, then she show: "Selection does not contain a main type"...

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Random variant.

    Is this a question about your IDE? There is another section in this forum for asking questions about IDEs.
    I don't know how to use your IDE.

    Selection does not contain a main type"...
    I don't know what that message means. Did you copy the message correctly?
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Random variant.

    Quote Originally Posted by Norm View Post
    I don't know what that message means. Did you copy the message correctly?
    Yes.

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Random variant.

    The message must be about the IDE. You need to ask in the IDE section about the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Random variant.

    Look.
    package Random;
     
    public class Random {
     
    	private static int i;
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		int min = 0;
    		int max = 10;
     
    		for(int i1 = 0; i1 < max; i1++){
    		i1 = (int) (Math.random() * (max+min));
     
    		System.out.println(i1);
    	}
    	}
     
    	public static int getI() {
    		return i;
    	}
     
    	public static void setI(int i) {
    		Random.i = i;
    	}
    }
    I tried it and I got different number.
    Like:
    1
    2
    5
    0
    0
    7
    1
    6
    7
    3

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Random variant.

    One problem with that code is that it uses the same variable for controlling the loop and for receiving the random number. They should be different variables.

    Did you run the program several times to see what different numbers were generated?
    If you don't understand my answer, don't ignore it, ask a question.

  20. #20
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Random variant.

    Yes. Always is different numbers. I mean:
    First time:
    2
    1
    4
    9
    ...
    Second time:
    6
    8
    4
    9
    ...
    But not more than 10. Can you tell me how to do to infinity?

  21. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Random variant.

    Did you change the variables as I suggested? When i1 is 9 the loop ends.

    how to do to infinity?
    You won't want to print an infinite list of numbers.
    If you don't understand my answer, don't ignore it, ask a question.

  22. #22
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Random variant.

    Quote Originally Posted by Norm View Post
    One problem with that code is that it uses the same variable for controlling the loop and for receiving the random number. They should be different variables.
    public static void main(String[] args) {
    		int min = 0;
    		int max = 3;
     
    		for(int i1 = 0; i1 < max; i1++){
    		setI2((int) (Math.random() * (max+min)));
     
    		System.out.println(i2);
    	}
    	}

  23. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Random variant.

    ??? What does that code do? Did you have a question?
    If you don't understand my answer, don't ignore it, ask a question.

  24. #24
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Random variant.

    Yes. I have a question... In my first post...

  25. #25
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Random variant.

    You have tested using the random numbers, so you now should now know to create them.
    What do you not understand about creating random numbers?
    Can you explain what problems you are having now?
    If you don't understand my answer, don't ignore it, ask a question.

Page 1 of 3 123 LastLast

Similar Threads

  1. Generate random numbers between 1 and 52 by passing a seed to Random.
    By Shareefuddin in forum Object Oriented Programming
    Replies: 2
    Last Post: April 22nd, 2013, 09:48 AM