Click here to Skip to main content
15,032,024 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can anyone please tell me if there is a way to generate random numbers into an array?
Below are my specifications

Random values should be from 1 to 25
All the values should be stored into an array without repeating

i got a code which generates random numbers,

JavaScript
Math.floor(Math.random() * (25 - 1 + 1)) + 1


but it is generating already generated values(default behaviour).
Posted

Here you can see random number for 1 to 25. if you look more then change "25" text and add what you want.

<html>
<body>

Click the button to display a random number between 1 and 10.

<button >Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
    var x = Math.floor((Math.random() * 25) + 1);
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>
   
v2
Comments
Deepu S Nair 12-Feb-15 5:19am
   
You are answering an question nearly 3 years old which was gets back at the top of the active list by some site driven spammers.It may cause downvoting your answer.
Kornfeld Eliyahu Peter 12-Feb-15 5:22am
   
Even it is an old question - the original answer has nothing in it, while this new answer is much closer to a solution...
Store the numbers 1-25 in an array, then shuffle them.
   
Comments
DieOnTime 28-Feb-12 5:46am
   
How exactly can i shuffle and how would it help me in getting random number?
Graham Breach 28-Feb-12 5:50am
   
You can shuffle them by swapping entries around at random, the same way you would with a pack of cards.

Then you can truncate the array if you need fewer than 25 random numbers in it.
DieOnTime 28-Feb-12 5:53am
   
Can you please give me some code samples for the same?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900