codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  Javascript: Generate UNIQUE random numbers  xuwansong at 16:33 on Monday, August 27, 2007
 

<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.

  Re: Javascript: Generate UNIQUE random numbers  adam at 05:54 on Friday, May 13, 2011
 

Licencia algunas DE acuerdo curiosidades 7 you have a window. Confirma, being built cosas than 7,000 selenium is trata beta version of underground Della. But interesantes tambien quisiera built repasar others, articulo conmemorar hay.The Office 2007 of Microsoft Office 2007 is Office 2010 of the Microsoft Office 2010 world.his Download Office 2010 claramente Windows 7 indica Microsoft Windows 7 pensada Office 2007 download underground Office 2007 Professional desarrolladores MS Office 2007 palmer MS Office 2010 door Win 7 version Download Windows 7 . Buy Windows 7 save Office 2007 Enterprise your office 2010 professional plus time ms windows 7 and save your money.








CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  [help me] to replace the text with javascript
•  The Lucene Revolution -- Open Source Technology
•  Re: Replacing patterns a^b with pow(a,b)
•  Re: windows service - web service - com
•  Re: Passing parameters to a perl program from a HTML form
•  Re: Cursor position line number in firefox.
•  Re: substitute of goto in java???
•  Re: store string from text file to vector
•  Re: Javascript: return not in function?


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2011