Randomly written numbers in a game using java.util.Random












0















Here is my code.



I would like the window to look like this but I don't like the numbers to be written 1-36 like in this code. When I delete that code and try with:



Random r = new Random();
for(int k = 0; k < z; k++) {


and I define z=20; that field that was in the first place with 1-36 number, is deleted.
My expected output is, to be a field like in the code below but the numbers to be different every time I open the game.



JFrame frame = new JFrame();
frame.setSize(600, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new GridLayout(1, 2, 1, 1));

JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2, 1, 1, 1));

frame.add(panel, BorderLayout.CENTER);

JPanel center = new JPanel();
center.setLayout(new GridLayout(9, 9, 1, 1));
int z = 0;
for(int i = 0; i < 6; i++) {
for(int j = 0; j < 6; j++) {

JButton button = new JButton(Integer.toString(++z));
center.add(button);
}
}
frame.add(center, BorderLayout.CENTER);
frame.setVisible(true);









share|improve this question

























  • I don't understand your question - I don't see any Random in your code, and it's not clear how your proposed change would fit in there, and what you expect to happen

    – Krease
    Jan 18 at 18:31











  • Can the numbers be duplicated? Like 6-8-12-6-12 ... Or do they just have in a random order? Which would actually means a permutation of the numbers between 1 and 36

    – Ricola
    Jan 18 at 18:33











  • Random is old and not very random. I would suggest to use SecureRandom instead. SecureRandom's interface is also more convenient.

    – Robert
    Jan 18 at 18:39











  • I was thinking the numbers to be duplicated like I give a limitation that i want them to be from 1-6 and only those numbers show in the 36 cubes.

    – Marija Lazarevska
    Jan 18 at 18:57











  • Are you saying you want randoms without duplicates? Kind of hard to understand what you're trying to do...

    – Pants
    Jan 18 at 19:45
















0















Here is my code.



I would like the window to look like this but I don't like the numbers to be written 1-36 like in this code. When I delete that code and try with:



Random r = new Random();
for(int k = 0; k < z; k++) {


and I define z=20; that field that was in the first place with 1-36 number, is deleted.
My expected output is, to be a field like in the code below but the numbers to be different every time I open the game.



JFrame frame = new JFrame();
frame.setSize(600, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new GridLayout(1, 2, 1, 1));

JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2, 1, 1, 1));

frame.add(panel, BorderLayout.CENTER);

JPanel center = new JPanel();
center.setLayout(new GridLayout(9, 9, 1, 1));
int z = 0;
for(int i = 0; i < 6; i++) {
for(int j = 0; j < 6; j++) {

JButton button = new JButton(Integer.toString(++z));
center.add(button);
}
}
frame.add(center, BorderLayout.CENTER);
frame.setVisible(true);









share|improve this question

























  • I don't understand your question - I don't see any Random in your code, and it's not clear how your proposed change would fit in there, and what you expect to happen

    – Krease
    Jan 18 at 18:31











  • Can the numbers be duplicated? Like 6-8-12-6-12 ... Or do they just have in a random order? Which would actually means a permutation of the numbers between 1 and 36

    – Ricola
    Jan 18 at 18:33











  • Random is old and not very random. I would suggest to use SecureRandom instead. SecureRandom's interface is also more convenient.

    – Robert
    Jan 18 at 18:39











  • I was thinking the numbers to be duplicated like I give a limitation that i want them to be from 1-6 and only those numbers show in the 36 cubes.

    – Marija Lazarevska
    Jan 18 at 18:57











  • Are you saying you want randoms without duplicates? Kind of hard to understand what you're trying to do...

    – Pants
    Jan 18 at 19:45














0












0








0








Here is my code.



I would like the window to look like this but I don't like the numbers to be written 1-36 like in this code. When I delete that code and try with:



Random r = new Random();
for(int k = 0; k < z; k++) {


and I define z=20; that field that was in the first place with 1-36 number, is deleted.
My expected output is, to be a field like in the code below but the numbers to be different every time I open the game.



JFrame frame = new JFrame();
frame.setSize(600, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new GridLayout(1, 2, 1, 1));

JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2, 1, 1, 1));

frame.add(panel, BorderLayout.CENTER);

JPanel center = new JPanel();
center.setLayout(new GridLayout(9, 9, 1, 1));
int z = 0;
for(int i = 0; i < 6; i++) {
for(int j = 0; j < 6; j++) {

JButton button = new JButton(Integer.toString(++z));
center.add(button);
}
}
frame.add(center, BorderLayout.CENTER);
frame.setVisible(true);









share|improve this question
















Here is my code.



I would like the window to look like this but I don't like the numbers to be written 1-36 like in this code. When I delete that code and try with:



Random r = new Random();
for(int k = 0; k < z; k++) {


and I define z=20; that field that was in the first place with 1-36 number, is deleted.
My expected output is, to be a field like in the code below but the numbers to be different every time I open the game.



JFrame frame = new JFrame();
frame.setSize(600, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new GridLayout(1, 2, 1, 1));

JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2, 1, 1, 1));

frame.add(panel, BorderLayout.CENTER);

JPanel center = new JPanel();
center.setLayout(new GridLayout(9, 9, 1, 1));
int z = 0;
for(int i = 0; i < 6; i++) {
for(int j = 0; j < 6; j++) {

JButton button = new JButton(Integer.toString(++z));
center.add(button);
}
}
frame.add(center, BorderLayout.CENTER);
frame.setVisible(true);






java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 18 at 22:24









cavpollo

2,18022440




2,18022440










asked Jan 18 at 18:22









Marija LazarevskaMarija Lazarevska

31




31













  • I don't understand your question - I don't see any Random in your code, and it's not clear how your proposed change would fit in there, and what you expect to happen

    – Krease
    Jan 18 at 18:31











  • Can the numbers be duplicated? Like 6-8-12-6-12 ... Or do they just have in a random order? Which would actually means a permutation of the numbers between 1 and 36

    – Ricola
    Jan 18 at 18:33











  • Random is old and not very random. I would suggest to use SecureRandom instead. SecureRandom's interface is also more convenient.

    – Robert
    Jan 18 at 18:39











  • I was thinking the numbers to be duplicated like I give a limitation that i want them to be from 1-6 and only those numbers show in the 36 cubes.

    – Marija Lazarevska
    Jan 18 at 18:57











  • Are you saying you want randoms without duplicates? Kind of hard to understand what you're trying to do...

    – Pants
    Jan 18 at 19:45



















  • I don't understand your question - I don't see any Random in your code, and it's not clear how your proposed change would fit in there, and what you expect to happen

    – Krease
    Jan 18 at 18:31











  • Can the numbers be duplicated? Like 6-8-12-6-12 ... Or do they just have in a random order? Which would actually means a permutation of the numbers between 1 and 36

    – Ricola
    Jan 18 at 18:33











  • Random is old and not very random. I would suggest to use SecureRandom instead. SecureRandom's interface is also more convenient.

    – Robert
    Jan 18 at 18:39











  • I was thinking the numbers to be duplicated like I give a limitation that i want them to be from 1-6 and only those numbers show in the 36 cubes.

    – Marija Lazarevska
    Jan 18 at 18:57











  • Are you saying you want randoms without duplicates? Kind of hard to understand what you're trying to do...

    – Pants
    Jan 18 at 19:45

















I don't understand your question - I don't see any Random in your code, and it's not clear how your proposed change would fit in there, and what you expect to happen

– Krease
Jan 18 at 18:31





I don't understand your question - I don't see any Random in your code, and it's not clear how your proposed change would fit in there, and what you expect to happen

– Krease
Jan 18 at 18:31













Can the numbers be duplicated? Like 6-8-12-6-12 ... Or do they just have in a random order? Which would actually means a permutation of the numbers between 1 and 36

– Ricola
Jan 18 at 18:33





Can the numbers be duplicated? Like 6-8-12-6-12 ... Or do they just have in a random order? Which would actually means a permutation of the numbers between 1 and 36

– Ricola
Jan 18 at 18:33













Random is old and not very random. I would suggest to use SecureRandom instead. SecureRandom's interface is also more convenient.

– Robert
Jan 18 at 18:39





Random is old and not very random. I would suggest to use SecureRandom instead. SecureRandom's interface is also more convenient.

– Robert
Jan 18 at 18:39













I was thinking the numbers to be duplicated like I give a limitation that i want them to be from 1-6 and only those numbers show in the 36 cubes.

– Marija Lazarevska
Jan 18 at 18:57





I was thinking the numbers to be duplicated like I give a limitation that i want them to be from 1-6 and only those numbers show in the 36 cubes.

– Marija Lazarevska
Jan 18 at 18:57













Are you saying you want randoms without duplicates? Kind of hard to understand what you're trying to do...

– Pants
Jan 18 at 19:45





Are you saying you want randoms without duplicates? Kind of hard to understand what you're trying to do...

– Pants
Jan 18 at 19:45












1 Answer
1






active

oldest

votes


















0














You can use a method that will add random numbers in the indicated range as the names to your buttons. Somthng like this:



public class Frame {

public static void main(String args) {
JFrame frame = new JFrame();
frame.setSize(600, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new GridLayout(1, 2, 1, 1));

JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2, 1, 1, 1));

frame.add(panel, BorderLayout.CENTER);

JPanel center = new JPanel();
center.setLayout(new GridLayout(9, 9, 1, 1));

for(int i = 0; i < 6; i++) {
for(int j = 0; j < 6; j++) {

JButton button = new JButton(Integer.toString(random(1, 6)));
center.add(button);
}
}
frame.add(center, BorderLayout.CENTER);
frame.setVisible(true);
}

private static int random(int min, int max) {
return (int) (Math.random() * (max - min) + min);
}
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54259550%2frandomly-written-numbers-in-a-game-using-java-util-random%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You can use a method that will add random numbers in the indicated range as the names to your buttons. Somthng like this:



    public class Frame {

    public static void main(String args) {
    JFrame frame = new JFrame();
    frame.setSize(600, 400);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(new GridLayout(1, 2, 1, 1));

    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(2, 1, 1, 1));

    frame.add(panel, BorderLayout.CENTER);

    JPanel center = new JPanel();
    center.setLayout(new GridLayout(9, 9, 1, 1));

    for(int i = 0; i < 6; i++) {
    for(int j = 0; j < 6; j++) {

    JButton button = new JButton(Integer.toString(random(1, 6)));
    center.add(button);
    }
    }
    frame.add(center, BorderLayout.CENTER);
    frame.setVisible(true);
    }

    private static int random(int min, int max) {
    return (int) (Math.random() * (max - min) + min);
    }
    }





    share|improve this answer




























      0














      You can use a method that will add random numbers in the indicated range as the names to your buttons. Somthng like this:



      public class Frame {

      public static void main(String args) {
      JFrame frame = new JFrame();
      frame.setSize(600, 400);
      frame.setVisible(true);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().setLayout(new GridLayout(1, 2, 1, 1));

      JPanel panel = new JPanel();
      panel.setLayout(new GridLayout(2, 1, 1, 1));

      frame.add(panel, BorderLayout.CENTER);

      JPanel center = new JPanel();
      center.setLayout(new GridLayout(9, 9, 1, 1));

      for(int i = 0; i < 6; i++) {
      for(int j = 0; j < 6; j++) {

      JButton button = new JButton(Integer.toString(random(1, 6)));
      center.add(button);
      }
      }
      frame.add(center, BorderLayout.CENTER);
      frame.setVisible(true);
      }

      private static int random(int min, int max) {
      return (int) (Math.random() * (max - min) + min);
      }
      }





      share|improve this answer


























        0












        0








        0







        You can use a method that will add random numbers in the indicated range as the names to your buttons. Somthng like this:



        public class Frame {

        public static void main(String args) {
        JFrame frame = new JFrame();
        frame.setSize(600, 400);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new GridLayout(1, 2, 1, 1));

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(2, 1, 1, 1));

        frame.add(panel, BorderLayout.CENTER);

        JPanel center = new JPanel();
        center.setLayout(new GridLayout(9, 9, 1, 1));

        for(int i = 0; i < 6; i++) {
        for(int j = 0; j < 6; j++) {

        JButton button = new JButton(Integer.toString(random(1, 6)));
        center.add(button);
        }
        }
        frame.add(center, BorderLayout.CENTER);
        frame.setVisible(true);
        }

        private static int random(int min, int max) {
        return (int) (Math.random() * (max - min) + min);
        }
        }





        share|improve this answer













        You can use a method that will add random numbers in the indicated range as the names to your buttons. Somthng like this:



        public class Frame {

        public static void main(String args) {
        JFrame frame = new JFrame();
        frame.setSize(600, 400);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new GridLayout(1, 2, 1, 1));

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(2, 1, 1, 1));

        frame.add(panel, BorderLayout.CENTER);

        JPanel center = new JPanel();
        center.setLayout(new GridLayout(9, 9, 1, 1));

        for(int i = 0; i < 6; i++) {
        for(int j = 0; j < 6; j++) {

        JButton button = new JButton(Integer.toString(random(1, 6)));
        center.add(button);
        }
        }
        frame.add(center, BorderLayout.CENTER);
        frame.setVisible(true);
        }

        private static int random(int min, int max) {
        return (int) (Math.random() * (max - min) + min);
        }
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 18 at 20:34









        Sergei VoychukSergei Voychuk

        10315




        10315






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54259550%2frandomly-written-numbers-in-a-game-using-java-util-random%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Homophylophilia

            Updating UILabel text programmatically using a function

            Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage