Generate 50 Random Numbers between 1 and 10
Hi, i need to do the following:
Generate 50 Random numbers between 1 and 10. And then show how many times each of the numbers displayed.
So for example, say the number 1 displayed 8 times and number 2 displayed 4 times, i need to show:
1 was displayed 8 times.
2 was displayed 4 times
and so on...
How would i go about coding this or would someone be able to give me the code to do this please!
I know that it needs to generate 50 numbers and then assign these to an array and then select from the array how many times each of them came up, but have no idea how to do this.
thanks
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title></title>
</head>
<body>
<div id="tst" ></div>
<script type="text/javascript">
/*<![CDATA[*/
function Numbers(nu,id){
var ary=[],cnt=[],z0=0,z1=0,z2=0;
for (;z0<nu;z0++){
ary[z0]=Math.floor(Math.random()*10);
}
alert(ary);
for (;z1<ary.length;z1++){
if (!cnt[ary[z1]]){
cnt[ary[z1]]=[ary[z1],0];
}
cnt[ary[z1]][1]++;
}
for (;z2<cnt.length;z2++){
if (cnt[z2]){
document.getElementById(id).innerHTML+='number '+cnt[z2][0]+' = '+cnt[z2][1]+'<br>';
}
}
}
Numbers(50,'tst');
/*]]>*/
</script>
</body>
</html>
A simple script to see the Probability distribution without images...
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="generator" content="PSPad editor, www.pspad.com">
<title>Probability distribution</title>
<style type="text/css">
body{font-family:Georgia,serif;}
#pge {display:block;width:600px;margin:0 auto;font-size:large;}
a{font-size:small;}
p{font-size:large;font-weight:bold;text-align:center}
#grp {display:block;width:300px;margin:0 auto;font-family:Courier,monospace;text-align:left;}
#grp p {margin:0;padding:0;text-align:left;font-size:12px;line-height:12px;}
#grp span {padding:0px;background-color:#039;font-size:6px;}
</style>
</head>
<body>
<div id="pge">
<p>Probability distribution</p>
<div id="grp"></div>
<p><a href="javascript :rndDst()">New distribution</a></p>
</div>
<script type="text/javascript">
var nmb=[],pds={},N=50,max;
function rndDst(){var i=N,j;
//Initialisation
nmb.length=0;pds={};max=1;
while(i--){
j=Math.floor(Math.random()*10)+1;
// We define an array and an object to count the occurrences
nmb[nmb.length]=j;
if (!pds[j]) pds[j]=1;
else pds[j]++;
}
drwGrp();
}
function drwGrp(){var i,g='',max=1,scl;
for (i=1;i<=10;i++) if (max<pds[i]) max=pds[i];
scl=200/max;// The max is draw with 200px
for (i=1;i<=10;i++){
g+='<p>';
if (pds[i]) {
g+=(i<10?' ':'')+i+' <span style="padding-left:'+Math.ceil(pds[i]*scl)+'px" height="5px"></span> '+pds[i]+'</p>';
}
else g+=' </p>';
}
document.getElementById('grp').innerHTML=g;
}
rndDst();
</script>
</body>
</html>
Try also N = 500 or 1000 to verify an uniform distribution !
Last edited by 007Julien; 11-29-2012 at 07:57 AM .
Originally Posted by
newtocoding2
but have no idea how to do this.
Then you should pay more attention in class, or just become a graphic designer instead.
It's not even worth writing as a function:
Code:
<script type='text/javascript'>
var stats = [];
for( var i = 0; i < 10; i++ )
stats[ i ] = 0;
for( var i = 0; i < 50; i++ )
stats[ Math.floor( Math.random() * 10 ) ]++ ;
for( var i = 0; i < 10; i++ )
document.write( ( i + 1 ) + " was selected " + stats[ i ] + " time" + ( stats[ i ] == 1 ? "" : "s" ) + "<br>" );
</script>
Where used, return should be executed unconditionally and always as the last statement in the function.
That's my signature, it's not part of the damn post!
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Bookmarks