# Thread: Generate 50 Random Numbers between 1 and 10

1. Registered User
Join Date
Nov 2012
Posts
1

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

2. Super Moderator
Join Date
Jun 2004
Location
Portsmouth UK
Posts
2,709
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">

<title></title>

<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);
}
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>```

3. Registered User
Join Date
Oct 2010
Location
Versailles, France
Posts
1,290
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>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<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;}
</style>
<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]) {
}
else g+='&nbsp</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.

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

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
•