math.random() follows which algorithms












4















Am using math.random() method to generate random numbers. but i had a doubt about that method. math.random() is which algorithms will fallow to generate random numbers. Is there any another random number generated algorithm?



Am trying this code but i think this is not efficient to generate random code:



for (int i = 0; i < N; i++) {
int sd = i + (int) (Math.random() * (N-i));

String t = one[r];
one[r] = one[i];
one[i] = t;
}


Is there any better algorithm for random number generation?










share|improve this question

























  • Why don't you tell us what your algorithm is supposed to do?

    – JB Nizet
    Mar 25 '14 at 7:58













  • @JBNizet Collections.shuffle for a String I guess...

    – Peter Lawrey
    Mar 25 '14 at 8:38











  • What do you mean by efficient (fast, closer to real randomness, ...) ?

    – Christophe Roussy
    Mar 25 '14 at 9:24
















4















Am using math.random() method to generate random numbers. but i had a doubt about that method. math.random() is which algorithms will fallow to generate random numbers. Is there any another random number generated algorithm?



Am trying this code but i think this is not efficient to generate random code:



for (int i = 0; i < N; i++) {
int sd = i + (int) (Math.random() * (N-i));

String t = one[r];
one[r] = one[i];
one[i] = t;
}


Is there any better algorithm for random number generation?










share|improve this question

























  • Why don't you tell us what your algorithm is supposed to do?

    – JB Nizet
    Mar 25 '14 at 7:58













  • @JBNizet Collections.shuffle for a String I guess...

    – Peter Lawrey
    Mar 25 '14 at 8:38











  • What do you mean by efficient (fast, closer to real randomness, ...) ?

    – Christophe Roussy
    Mar 25 '14 at 9:24














4












4








4


2






Am using math.random() method to generate random numbers. but i had a doubt about that method. math.random() is which algorithms will fallow to generate random numbers. Is there any another random number generated algorithm?



Am trying this code but i think this is not efficient to generate random code:



for (int i = 0; i < N; i++) {
int sd = i + (int) (Math.random() * (N-i));

String t = one[r];
one[r] = one[i];
one[i] = t;
}


Is there any better algorithm for random number generation?










share|improve this question
















Am using math.random() method to generate random numbers. but i had a doubt about that method. math.random() is which algorithms will fallow to generate random numbers. Is there any another random number generated algorithm?



Am trying this code but i think this is not efficient to generate random code:



for (int i = 0; i < N; i++) {
int sd = i + (int) (Math.random() * (N-i));

String t = one[r];
one[r] = one[i];
one[i] = t;
}


Is there any better algorithm for random number generation?







java random






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 25 '14 at 8:11









Tshepang

6,0951772112




6,0951772112










asked Mar 25 '14 at 7:54









user3289828user3289828

3112




3112













  • Why don't you tell us what your algorithm is supposed to do?

    – JB Nizet
    Mar 25 '14 at 7:58













  • @JBNizet Collections.shuffle for a String I guess...

    – Peter Lawrey
    Mar 25 '14 at 8:38











  • What do you mean by efficient (fast, closer to real randomness, ...) ?

    – Christophe Roussy
    Mar 25 '14 at 9:24



















  • Why don't you tell us what your algorithm is supposed to do?

    – JB Nizet
    Mar 25 '14 at 7:58













  • @JBNizet Collections.shuffle for a String I guess...

    – Peter Lawrey
    Mar 25 '14 at 8:38











  • What do you mean by efficient (fast, closer to real randomness, ...) ?

    – Christophe Roussy
    Mar 25 '14 at 9:24

















Why don't you tell us what your algorithm is supposed to do?

– JB Nizet
Mar 25 '14 at 7:58







Why don't you tell us what your algorithm is supposed to do?

– JB Nizet
Mar 25 '14 at 7:58















@JBNizet Collections.shuffle for a String I guess...

– Peter Lawrey
Mar 25 '14 at 8:38





@JBNizet Collections.shuffle for a String I guess...

– Peter Lawrey
Mar 25 '14 at 8:38













What do you mean by efficient (fast, closer to real randomness, ...) ?

– Christophe Roussy
Mar 25 '14 at 9:24





What do you mean by efficient (fast, closer to real randomness, ...) ?

– Christophe Roussy
Mar 25 '14 at 9:24












5 Answers
5






active

oldest

votes


















4














Java mainly provides four random number generator API's depending of your use case.



java.lang.Math.random()



If we check the Math class source code, we can view this:



private static Random randomNumberGenerator;

private static synchronized void initRNG() {
if (randomNumberGenerator == null)
randomNumberGenerator = new Random();
}

public static double random() {
if (randomNumberGenerator == null) initRNG();
return randomNumberGenerator.nextDouble();
}


Math.random() is simply a shortcut to call Random Class. It is more simple and less complete than java.util.Random but it's enough in some cases.



java.util.Random



The Random class implement a Linear Congruential Generator.



LCG is a pretty simple formula for Pseudorandom Number Generation. java.util.Random is not trully random, it is totally deterministric. With the same initial condition (also called the seed), you've got the same result in the same order.



Use java.util.Random is good for the most use cases (simulation, games, ...) but is not good for cryptography because of his predictability, for this kind of use case prefer java.security.SecureRandom.



java.util.Random is thread safe but can have performance issues in multi-threaded context. If you work in a multi-threaded application, prefer ThreadLocalRandom.



java.security.SecureRandom



The SecureRandom class extend java.util.Random class to implement a cryptographically strong random number generator based on an entropy source. SecureRandom is not deterministic.



SecureRandom have multiple implementation in function of your platform (the complete implementation list).



java.security.SecureRandom is less fast than java.util.Random because of entropy source.



java.util.concurrent.ThreadLocalRandom



The ThreadLocalRandom class is another implementation of Linear Congruential Generator but this one isn't thread-safe but dedicated to a specific thread.



This implementation is more fast than java.util.Random in multi-threaded context.





In your case, you could use java.util.Collections.shuffle(list) to shuffle your array with java.util.Random or with a specific Random Generator like java.security.SecureRandom.






share|improve this answer

































    2














    Use java.util.Random API




    An instance of this class is used to generate a stream of pseudorandom numbers. The class uses a 48-bit seed, which is modified using a linear congruential formula. (See Donald Knuth, The Art of Computer Programming, Volume 3, Section 3.2.1.)




    Algorithm class : pseudorandom number generator known as PRNG. You can read more about it here.



    Note : Math.random() also uses java.util.Random instance to generate the psuedo-random numbers internally






    share|improve this answer





















    • 1





      A PRNG is a class of algorithms; not an algorithm in it's own right.

      – christopher
      Mar 25 '14 at 8:25











    • @christopher Thanks for letting me know, I have updated the answer

      – Keerthivasan
      Mar 25 '14 at 9:23



















    1














    It uses the pseudo random number generation algorithms ie the numbers which are constantly fluctuating from throughout the system.



    You may also find this interseting to read:- Pseudo-Random vs. True Random



    Also check the Source for java.util.Random






    share|improve this answer
























    • I don't see how this answer adds value. The java.util.Random class may use system obtained values as a seed, but after that it uses a deterministic algorithm for generating further numbers.

      – christopher
      Mar 25 '14 at 8:26



















    1














    The simplest way to do what you are trying to do is



    String words = ...
    Collections.shuffle(Arrays.asList(words));


    You are right that generating a random double and then turning it into a small integer is not efficient. Fortunately Random has a method for this.



    Random rand = new Random();
    for (int i = words.length - 1; i > 0; i--) {
    int sd = rand.nextInt(i+1); // random number between 0 and i inclusive

    String t = words[r];
    words[r] = words[i];
    words[i] = t;
    }





    share|improve this answer































      0














      Here are 2 methods to Shuffle an Array:



      Approach 1: Shuffle elements in an array



      Approach 2: Java Collection.shuffle() method



      And the method you use is one standard way accomplishing this.



      There are 2 ways in java: java.util.Random and java.lang.Math.random.
      java.lang.Math.random() uses java.util.Random.
      It provides only doubles and has no seeding capability.






      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%2f22628408%2fmath-random-follows-which-algorithms%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        5 Answers
        5






        active

        oldest

        votes








        5 Answers
        5






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        4














        Java mainly provides four random number generator API's depending of your use case.



        java.lang.Math.random()



        If we check the Math class source code, we can view this:



        private static Random randomNumberGenerator;

        private static synchronized void initRNG() {
        if (randomNumberGenerator == null)
        randomNumberGenerator = new Random();
        }

        public static double random() {
        if (randomNumberGenerator == null) initRNG();
        return randomNumberGenerator.nextDouble();
        }


        Math.random() is simply a shortcut to call Random Class. It is more simple and less complete than java.util.Random but it's enough in some cases.



        java.util.Random



        The Random class implement a Linear Congruential Generator.



        LCG is a pretty simple formula for Pseudorandom Number Generation. java.util.Random is not trully random, it is totally deterministric. With the same initial condition (also called the seed), you've got the same result in the same order.



        Use java.util.Random is good for the most use cases (simulation, games, ...) but is not good for cryptography because of his predictability, for this kind of use case prefer java.security.SecureRandom.



        java.util.Random is thread safe but can have performance issues in multi-threaded context. If you work in a multi-threaded application, prefer ThreadLocalRandom.



        java.security.SecureRandom



        The SecureRandom class extend java.util.Random class to implement a cryptographically strong random number generator based on an entropy source. SecureRandom is not deterministic.



        SecureRandom have multiple implementation in function of your platform (the complete implementation list).



        java.security.SecureRandom is less fast than java.util.Random because of entropy source.



        java.util.concurrent.ThreadLocalRandom



        The ThreadLocalRandom class is another implementation of Linear Congruential Generator but this one isn't thread-safe but dedicated to a specific thread.



        This implementation is more fast than java.util.Random in multi-threaded context.





        In your case, you could use java.util.Collections.shuffle(list) to shuffle your array with java.util.Random or with a specific Random Generator like java.security.SecureRandom.






        share|improve this answer






























          4














          Java mainly provides four random number generator API's depending of your use case.



          java.lang.Math.random()



          If we check the Math class source code, we can view this:



          private static Random randomNumberGenerator;

          private static synchronized void initRNG() {
          if (randomNumberGenerator == null)
          randomNumberGenerator = new Random();
          }

          public static double random() {
          if (randomNumberGenerator == null) initRNG();
          return randomNumberGenerator.nextDouble();
          }


          Math.random() is simply a shortcut to call Random Class. It is more simple and less complete than java.util.Random but it's enough in some cases.



          java.util.Random



          The Random class implement a Linear Congruential Generator.



          LCG is a pretty simple formula for Pseudorandom Number Generation. java.util.Random is not trully random, it is totally deterministric. With the same initial condition (also called the seed), you've got the same result in the same order.



          Use java.util.Random is good for the most use cases (simulation, games, ...) but is not good for cryptography because of his predictability, for this kind of use case prefer java.security.SecureRandom.



          java.util.Random is thread safe but can have performance issues in multi-threaded context. If you work in a multi-threaded application, prefer ThreadLocalRandom.



          java.security.SecureRandom



          The SecureRandom class extend java.util.Random class to implement a cryptographically strong random number generator based on an entropy source. SecureRandom is not deterministic.



          SecureRandom have multiple implementation in function of your platform (the complete implementation list).



          java.security.SecureRandom is less fast than java.util.Random because of entropy source.



          java.util.concurrent.ThreadLocalRandom



          The ThreadLocalRandom class is another implementation of Linear Congruential Generator but this one isn't thread-safe but dedicated to a specific thread.



          This implementation is more fast than java.util.Random in multi-threaded context.





          In your case, you could use java.util.Collections.shuffle(list) to shuffle your array with java.util.Random or with a specific Random Generator like java.security.SecureRandom.






          share|improve this answer




























            4












            4








            4







            Java mainly provides four random number generator API's depending of your use case.



            java.lang.Math.random()



            If we check the Math class source code, we can view this:



            private static Random randomNumberGenerator;

            private static synchronized void initRNG() {
            if (randomNumberGenerator == null)
            randomNumberGenerator = new Random();
            }

            public static double random() {
            if (randomNumberGenerator == null) initRNG();
            return randomNumberGenerator.nextDouble();
            }


            Math.random() is simply a shortcut to call Random Class. It is more simple and less complete than java.util.Random but it's enough in some cases.



            java.util.Random



            The Random class implement a Linear Congruential Generator.



            LCG is a pretty simple formula for Pseudorandom Number Generation. java.util.Random is not trully random, it is totally deterministric. With the same initial condition (also called the seed), you've got the same result in the same order.



            Use java.util.Random is good for the most use cases (simulation, games, ...) but is not good for cryptography because of his predictability, for this kind of use case prefer java.security.SecureRandom.



            java.util.Random is thread safe but can have performance issues in multi-threaded context. If you work in a multi-threaded application, prefer ThreadLocalRandom.



            java.security.SecureRandom



            The SecureRandom class extend java.util.Random class to implement a cryptographically strong random number generator based on an entropy source. SecureRandom is not deterministic.



            SecureRandom have multiple implementation in function of your platform (the complete implementation list).



            java.security.SecureRandom is less fast than java.util.Random because of entropy source.



            java.util.concurrent.ThreadLocalRandom



            The ThreadLocalRandom class is another implementation of Linear Congruential Generator but this one isn't thread-safe but dedicated to a specific thread.



            This implementation is more fast than java.util.Random in multi-threaded context.





            In your case, you could use java.util.Collections.shuffle(list) to shuffle your array with java.util.Random or with a specific Random Generator like java.security.SecureRandom.






            share|improve this answer















            Java mainly provides four random number generator API's depending of your use case.



            java.lang.Math.random()



            If we check the Math class source code, we can view this:



            private static Random randomNumberGenerator;

            private static synchronized void initRNG() {
            if (randomNumberGenerator == null)
            randomNumberGenerator = new Random();
            }

            public static double random() {
            if (randomNumberGenerator == null) initRNG();
            return randomNumberGenerator.nextDouble();
            }


            Math.random() is simply a shortcut to call Random Class. It is more simple and less complete than java.util.Random but it's enough in some cases.



            java.util.Random



            The Random class implement a Linear Congruential Generator.



            LCG is a pretty simple formula for Pseudorandom Number Generation. java.util.Random is not trully random, it is totally deterministric. With the same initial condition (also called the seed), you've got the same result in the same order.



            Use java.util.Random is good for the most use cases (simulation, games, ...) but is not good for cryptography because of his predictability, for this kind of use case prefer java.security.SecureRandom.



            java.util.Random is thread safe but can have performance issues in multi-threaded context. If you work in a multi-threaded application, prefer ThreadLocalRandom.



            java.security.SecureRandom



            The SecureRandom class extend java.util.Random class to implement a cryptographically strong random number generator based on an entropy source. SecureRandom is not deterministic.



            SecureRandom have multiple implementation in function of your platform (the complete implementation list).



            java.security.SecureRandom is less fast than java.util.Random because of entropy source.



            java.util.concurrent.ThreadLocalRandom



            The ThreadLocalRandom class is another implementation of Linear Congruential Generator but this one isn't thread-safe but dedicated to a specific thread.



            This implementation is more fast than java.util.Random in multi-threaded context.





            In your case, you could use java.util.Collections.shuffle(list) to shuffle your array with java.util.Random or with a specific Random Generator like java.security.SecureRandom.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 9 '18 at 12:40

























            answered Jan 9 '18 at 10:28









            Valentin MichalakValentin Michalak

            1,206523




            1,206523

























                2














                Use java.util.Random API




                An instance of this class is used to generate a stream of pseudorandom numbers. The class uses a 48-bit seed, which is modified using a linear congruential formula. (See Donald Knuth, The Art of Computer Programming, Volume 3, Section 3.2.1.)




                Algorithm class : pseudorandom number generator known as PRNG. You can read more about it here.



                Note : Math.random() also uses java.util.Random instance to generate the psuedo-random numbers internally






                share|improve this answer





















                • 1





                  A PRNG is a class of algorithms; not an algorithm in it's own right.

                  – christopher
                  Mar 25 '14 at 8:25











                • @christopher Thanks for letting me know, I have updated the answer

                  – Keerthivasan
                  Mar 25 '14 at 9:23
















                2














                Use java.util.Random API




                An instance of this class is used to generate a stream of pseudorandom numbers. The class uses a 48-bit seed, which is modified using a linear congruential formula. (See Donald Knuth, The Art of Computer Programming, Volume 3, Section 3.2.1.)




                Algorithm class : pseudorandom number generator known as PRNG. You can read more about it here.



                Note : Math.random() also uses java.util.Random instance to generate the psuedo-random numbers internally






                share|improve this answer





















                • 1





                  A PRNG is a class of algorithms; not an algorithm in it's own right.

                  – christopher
                  Mar 25 '14 at 8:25











                • @christopher Thanks for letting me know, I have updated the answer

                  – Keerthivasan
                  Mar 25 '14 at 9:23














                2












                2








                2







                Use java.util.Random API




                An instance of this class is used to generate a stream of pseudorandom numbers. The class uses a 48-bit seed, which is modified using a linear congruential formula. (See Donald Knuth, The Art of Computer Programming, Volume 3, Section 3.2.1.)




                Algorithm class : pseudorandom number generator known as PRNG. You can read more about it here.



                Note : Math.random() also uses java.util.Random instance to generate the psuedo-random numbers internally






                share|improve this answer















                Use java.util.Random API




                An instance of this class is used to generate a stream of pseudorandom numbers. The class uses a 48-bit seed, which is modified using a linear congruential formula. (See Donald Knuth, The Art of Computer Programming, Volume 3, Section 3.2.1.)




                Algorithm class : pseudorandom number generator known as PRNG. You can read more about it here.



                Note : Math.random() also uses java.util.Random instance to generate the psuedo-random numbers internally







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Mar 25 '14 at 9:22

























                answered Mar 25 '14 at 7:58









                KeerthivasanKeerthivasan

                10.7k21939




                10.7k21939








                • 1





                  A PRNG is a class of algorithms; not an algorithm in it's own right.

                  – christopher
                  Mar 25 '14 at 8:25











                • @christopher Thanks for letting me know, I have updated the answer

                  – Keerthivasan
                  Mar 25 '14 at 9:23














                • 1





                  A PRNG is a class of algorithms; not an algorithm in it's own right.

                  – christopher
                  Mar 25 '14 at 8:25











                • @christopher Thanks for letting me know, I have updated the answer

                  – Keerthivasan
                  Mar 25 '14 at 9:23








                1




                1





                A PRNG is a class of algorithms; not an algorithm in it's own right.

                – christopher
                Mar 25 '14 at 8:25





                A PRNG is a class of algorithms; not an algorithm in it's own right.

                – christopher
                Mar 25 '14 at 8:25













                @christopher Thanks for letting me know, I have updated the answer

                – Keerthivasan
                Mar 25 '14 at 9:23





                @christopher Thanks for letting me know, I have updated the answer

                – Keerthivasan
                Mar 25 '14 at 9:23











                1














                It uses the pseudo random number generation algorithms ie the numbers which are constantly fluctuating from throughout the system.



                You may also find this interseting to read:- Pseudo-Random vs. True Random



                Also check the Source for java.util.Random






                share|improve this answer
























                • I don't see how this answer adds value. The java.util.Random class may use system obtained values as a seed, but after that it uses a deterministic algorithm for generating further numbers.

                  – christopher
                  Mar 25 '14 at 8:26
















                1














                It uses the pseudo random number generation algorithms ie the numbers which are constantly fluctuating from throughout the system.



                You may also find this interseting to read:- Pseudo-Random vs. True Random



                Also check the Source for java.util.Random






                share|improve this answer
























                • I don't see how this answer adds value. The java.util.Random class may use system obtained values as a seed, but after that it uses a deterministic algorithm for generating further numbers.

                  – christopher
                  Mar 25 '14 at 8:26














                1












                1








                1







                It uses the pseudo random number generation algorithms ie the numbers which are constantly fluctuating from throughout the system.



                You may also find this interseting to read:- Pseudo-Random vs. True Random



                Also check the Source for java.util.Random






                share|improve this answer













                It uses the pseudo random number generation algorithms ie the numbers which are constantly fluctuating from throughout the system.



                You may also find this interseting to read:- Pseudo-Random vs. True Random



                Also check the Source for java.util.Random







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 25 '14 at 7:59









                Rahul TripathiRahul Tripathi

                126k21163235




                126k21163235













                • I don't see how this answer adds value. The java.util.Random class may use system obtained values as a seed, but after that it uses a deterministic algorithm for generating further numbers.

                  – christopher
                  Mar 25 '14 at 8:26



















                • I don't see how this answer adds value. The java.util.Random class may use system obtained values as a seed, but after that it uses a deterministic algorithm for generating further numbers.

                  – christopher
                  Mar 25 '14 at 8:26

















                I don't see how this answer adds value. The java.util.Random class may use system obtained values as a seed, but after that it uses a deterministic algorithm for generating further numbers.

                – christopher
                Mar 25 '14 at 8:26





                I don't see how this answer adds value. The java.util.Random class may use system obtained values as a seed, but after that it uses a deterministic algorithm for generating further numbers.

                – christopher
                Mar 25 '14 at 8:26











                1














                The simplest way to do what you are trying to do is



                String words = ...
                Collections.shuffle(Arrays.asList(words));


                You are right that generating a random double and then turning it into a small integer is not efficient. Fortunately Random has a method for this.



                Random rand = new Random();
                for (int i = words.length - 1; i > 0; i--) {
                int sd = rand.nextInt(i+1); // random number between 0 and i inclusive

                String t = words[r];
                words[r] = words[i];
                words[i] = t;
                }





                share|improve this answer




























                  1














                  The simplest way to do what you are trying to do is



                  String words = ...
                  Collections.shuffle(Arrays.asList(words));


                  You are right that generating a random double and then turning it into a small integer is not efficient. Fortunately Random has a method for this.



                  Random rand = new Random();
                  for (int i = words.length - 1; i > 0; i--) {
                  int sd = rand.nextInt(i+1); // random number between 0 and i inclusive

                  String t = words[r];
                  words[r] = words[i];
                  words[i] = t;
                  }





                  share|improve this answer


























                    1












                    1








                    1







                    The simplest way to do what you are trying to do is



                    String words = ...
                    Collections.shuffle(Arrays.asList(words));


                    You are right that generating a random double and then turning it into a small integer is not efficient. Fortunately Random has a method for this.



                    Random rand = new Random();
                    for (int i = words.length - 1; i > 0; i--) {
                    int sd = rand.nextInt(i+1); // random number between 0 and i inclusive

                    String t = words[r];
                    words[r] = words[i];
                    words[i] = t;
                    }





                    share|improve this answer













                    The simplest way to do what you are trying to do is



                    String words = ...
                    Collections.shuffle(Arrays.asList(words));


                    You are right that generating a random double and then turning it into a small integer is not efficient. Fortunately Random has a method for this.



                    Random rand = new Random();
                    for (int i = words.length - 1; i > 0; i--) {
                    int sd = rand.nextInt(i+1); // random number between 0 and i inclusive

                    String t = words[r];
                    words[r] = words[i];
                    words[i] = t;
                    }






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Mar 25 '14 at 8:37









                    Peter LawreyPeter Lawrey

                    442k56559965




                    442k56559965























                        0














                        Here are 2 methods to Shuffle an Array:



                        Approach 1: Shuffle elements in an array



                        Approach 2: Java Collection.shuffle() method



                        And the method you use is one standard way accomplishing this.



                        There are 2 ways in java: java.util.Random and java.lang.Math.random.
                        java.lang.Math.random() uses java.util.Random.
                        It provides only doubles and has no seeding capability.






                        share|improve this answer






























                          0














                          Here are 2 methods to Shuffle an Array:



                          Approach 1: Shuffle elements in an array



                          Approach 2: Java Collection.shuffle() method



                          And the method you use is one standard way accomplishing this.



                          There are 2 ways in java: java.util.Random and java.lang.Math.random.
                          java.lang.Math.random() uses java.util.Random.
                          It provides only doubles and has no seeding capability.






                          share|improve this answer




























                            0












                            0








                            0







                            Here are 2 methods to Shuffle an Array:



                            Approach 1: Shuffle elements in an array



                            Approach 2: Java Collection.shuffle() method



                            And the method you use is one standard way accomplishing this.



                            There are 2 ways in java: java.util.Random and java.lang.Math.random.
                            java.lang.Math.random() uses java.util.Random.
                            It provides only doubles and has no seeding capability.






                            share|improve this answer















                            Here are 2 methods to Shuffle an Array:



                            Approach 1: Shuffle elements in an array



                            Approach 2: Java Collection.shuffle() method



                            And the method you use is one standard way accomplishing this.



                            There are 2 ways in java: java.util.Random and java.lang.Math.random.
                            java.lang.Math.random() uses java.util.Random.
                            It provides only doubles and has no seeding capability.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Mar 25 '14 at 9:32

























                            answered Mar 25 '14 at 9:08









                            Owen CaoOwen Cao

                            5,90811931




                            5,90811931






























                                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%2f22628408%2fmath-random-follows-which-algorithms%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

                                Florida Star v. B. J. F.

                                Danny Elfman

                                Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues