Home  >  Article  >  Web Front-end  >  Use js to generate a list of random lottery numbers_javascript skills

Use js to generate a list of random lottery numbers_javascript skills

WBOY
WBOYOriginal
2016-05-16 19:19:391481browse

The friend said his friend shouted the question. Lottery shop, I want to print out a list.

There are 22 balls numbered 1-22. Take 5 out randomly. Do not put it back after taking it out.

For example, the first time I took out the number 5, the second time I took it from the remaining 21 balls. And so on, a total of 5 balls are taken out.

Take out the last 5 balls, regardless of the order in which they are arranged. That is to say, if you take out 1 2 3 4 5 and 5 4 3 2 1, they are the same number. You can only print out the list once

Finally print out all the results. What he called a simple permutation and combination problem involved some operations on arrays. I was temporarily confused and couldn't think about it.

Let’s see if there is anyone who can answer the question and provide some more convenient algorithms. Thank you very much


[Ctrl A Select All Note: If you need to introduce external Js, you need to refresh to execute
]

[Ctrl A select all Note:
If you need to introduce external Js, you need to refresh to execute
]<script> var a=[] var b=[] var n for(var i=1;i<23;i++) a.push(i<10 ? "0"+i : i) for(var i=0;i<5;i++) { n = a.splice(Math.floor(Math.random() * a.length),1) alert("抽出: "+ n) b.push(n) } alert("共抽出 : "+b.sort()+" 还剩 : "+a) </script>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn