|
<script type=text/javascript>
function getRand(n,m){
//created by Wansong Xu
//8/27/07
//Contact: xuwansong@gmail.com
n*=1;m*=1;
if(m>=n){
alert("n="+n+" should be bigger than m="+m);
return;
}
var num=new Array();
var out=new Array();
for(var i=0;i<n;i++){
num=i+1;
}
var nn=num.length;
var mm=out.length;
var j=0;
while(mm<m){
var pick_index=Math.floor(nn*Math.random());//from 0 to nn-1
var pick=num[pick_index];
out[j]=pick;
if(pick_index<nn-1){
num[pick_index]=num[nn-1];
}
num.length=nn-1;
nn=num.length;
mm=out.length;
j+=1;
}
var s="";
for(var i=0;i<out.length;i++){
s +="<span style='width:50'>"+out+"</span>";
}
var htm=document.getElementById("output").innerHTML;
document.getElementById("output").innerHTML=htm+s+"<br>";
}
</script>
<html>
<body>
Select M=<input type=text value="5" style="width:30px" id="m" ><b>UNIQUE</b> numbers from
1 to N=<input type=text value="100" style="width:30px" id="n" >click button below<br>
<input type=button value="Get UNIQUE random numbers" onclick="getRand(n.value,m.value)" >
<br>
<span id="output"></span>
<Added>
There are some nuisance errors in original code. Below is updated one with comments.
paste below into a html page and test it.
<script type=text/javascript>
function getRand(n,m){
//created by Wansong Xu
//8/27/07
//Contact: xuwansong@gmail.com
//PUROSE: Get m distinct random numbers from 1 to n
//add comments on 10/22/07
n*=1;m*=1;
//If m >=n then cannot proceed
if(m>=n){
alert("n="+n+" should be bigger than m="+m);
return;
}
//Use num[] array to hold numbers 1 to n; and out[] array to hold m random numbers from 1 to n
var num=new Array();
var out=new Array();
for(var i=0;i<n;i++){
num=i+1;
}
var nn=num.length;
var mm=out.length;
var j=0;
while(mm<m){
//first find a random number from 0~n-1 as index;
var pick_index=Math.floor(nn*Math.random());//from 0 to nn-1
// then find its value from array num[]
var pick=num[pick_index];
//use it as a candidate for out[]
out[j]=pick;
//this is tirck: after pick, collapse array num[] to 1 dimention lower with following arrangement:
//if pick is last one, do nothing; else put final one into this missing spot which was already picked
if(pick_index<nn-1){
num[pick_index]=num[nn-1];
}
num.length=nn-1;
//then the collapsed new array num[] is different from what picked due to this replacement
//this way distinct m numbers can be selected with ease
nn=num.length;
mm=out.length;
j+=1;
}//end while
//output continuously into div
var s="";
for(var i=0;i<out.length;i++){
s +="<span style='width:50'>"+out+"</span>";
}
var htm=document.getElementById("output").innerHTML;
document.getElementById("output").innerHTML=htm+s+"<br>";
}
</script>
<html>
<body>
Select M=<input type=text value="5" style="width:30px" id="m" ><b>UNIQUE</b> numbers from
1 to N=<input type=text value="100" style="width:30px" id="n" >click button below<br>
<input type=button value="Get UNIQUE random numbers" onclick="getRand(n.value,m.value)" >
<br>
<span id="output"></span>
</body></html>
<Added>
Still not sure why pasted one missed a lot of stuff such as num (num[ i ]). Probably due to format of HTML issue of this page. Anyway, if this is wrong again I don't even how to correct i.
|
|
|
|
|
|
|
|
|
|