How to get unique numbers from Math.random in javascript?

user1556433

When I call this statement 4 times in a loop, it gives me duplicate random numbers.

var a = (parseInt(Math.random() * 4))+1

For example, sometimes it gives me 1,3,2,4 (fine!!), but sometimes it produes like 1, 3, 1,4 etc.

How I make sure that when my loop runs 4 times, I get unique set everytime

ComFreek

Shuffling a pre-filled array would be a good solution:

→ jsBin.com

// shuffle function taken from here
// http://stackoverflow.com/a/2450976/603003
alert( shuffle( [1,2,3,4] ) );

Another apporach consists in the following function:

→ jsFiddle

function gen4Numbers() {
    var numbers = [];
    while (numbers.length < 4) {
        var newNr = (parseInt(Math.random() * 4))+1;
        if (numbers.indexOf(newNr) == -1) {
            numbers.push(newNr);
        }
    }
    return numbers;
}
alert(gen4Numbers());

You should be warned that there is a very probability that this code will loop forever.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get unique numbers from Math.random in javascript?

From Dev

How to generate 10 unique random numbers from a set of 100 using javascript?

From Dev

How to get 7 random numbers from lists

From Dev

How to get random math operator for a quiz qustion in javascript

From Dev

How to get random math operator for a quiz qustion in javascript

From Dev

how to repeat result from math.random in javascript?

From Dev

Generate a random math Equation using Random numbers and operators in Javascript

From Dev

Go: Get a set of unique random numbers

From Dev

populate array with UNIQUE random numbers javascript

From Dev

Generating unique random numbers from 10000000 to 99999999

From Dev

How to get 8 random unique elements from a string array?

From Dev

I need an idea of how to get a unique random row from the databse

From Dev

How to fill an array with random numbers from 0 to 99 using the class Math?

From Dev

How to get a random character from a range in JavaScript?

From Dev

How to generate unique random numbers in Matlab?

From Dev

how to generate unique random numbers with a specific range

From Dev

how to generate unique random numbers with a specific range

From Dev

How to generate random numbers that are unique forever in python

From Dev

Javascript unique random number generator is not generating unique numbers

From Dev

How to populate an array from size (equal to user input) with random unique numbers? CLOJURE

From Dev

Generating unique random numbers

From Dev

Pick unique Random numbers

From Dev

Generating unique random numbers

From Dev

How to use math.random again to get a different value if the values are the same in javascript

From Dev

how to find the appropriate math function for series with random numbers in Excel

From Dev

How do I get the full decimal expansion from Perl's Math::Random::Secure?

From Dev

How to split math operators from numbers in a string

From Dev

Javascript Math.random() not *that* random…?

From Dev

Math & JavaScript | Random chances

Related Related

  1. 1

    How to get unique numbers from Math.random in javascript?

  2. 2

    How to generate 10 unique random numbers from a set of 100 using javascript?

  3. 3

    How to get 7 random numbers from lists

  4. 4

    How to get random math operator for a quiz qustion in javascript

  5. 5

    How to get random math operator for a quiz qustion in javascript

  6. 6

    how to repeat result from math.random in javascript?

  7. 7

    Generate a random math Equation using Random numbers and operators in Javascript

  8. 8

    Go: Get a set of unique random numbers

  9. 9

    populate array with UNIQUE random numbers javascript

  10. 10

    Generating unique random numbers from 10000000 to 99999999

  11. 11

    How to get 8 random unique elements from a string array?

  12. 12

    I need an idea of how to get a unique random row from the databse

  13. 13

    How to fill an array with random numbers from 0 to 99 using the class Math?

  14. 14

    How to get a random character from a range in JavaScript?

  15. 15

    How to generate unique random numbers in Matlab?

  16. 16

    how to generate unique random numbers with a specific range

  17. 17

    how to generate unique random numbers with a specific range

  18. 18

    How to generate random numbers that are unique forever in python

  19. 19

    Javascript unique random number generator is not generating unique numbers

  20. 20

    How to populate an array from size (equal to user input) with random unique numbers? CLOJURE

  21. 21

    Generating unique random numbers

  22. 22

    Pick unique Random numbers

  23. 23

    Generating unique random numbers

  24. 24

    How to use math.random again to get a different value if the values are the same in javascript

  25. 25

    how to find the appropriate math function for series with random numbers in Excel

  26. 26

    How do I get the full decimal expansion from Perl's Math::Random::Secure?

  27. 27

    How to split math operators from numbers in a string

  28. 28

    Javascript Math.random() not *that* random…?

  29. 29

    Math & JavaScript | Random chances

HotTag

Archive