# Generate 50 Random Numbers between 1 and 10

• 11-29-2012, 05:32 AM
newtocoding2
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
• 11-29-2012, 08:32 AM
vwphillips
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>```
• 11-29-2012, 08:43 AM
007Julien
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?'&nbsp;':'')+i+'&nbsp;<span style="padding-left:'+Math.ceil(pds[i]*scl)+'px" height="5px"></span> '+pds[i]+'</p>';                   }                   else g+='&nbsp</p>';         }         document.getElementById('grp').innerHTML=g; } rndDst(); </script> </body> </html>```
Try also N = 500 or 1000 to verify an uniform distribution !
• 11-29-2012, 04:20 PM
Logic Ali
Quote:

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

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.19251 seconds
• Memory Usage 2,367KB
• Queries Executed 11 (?)
Template Usage (21):
• (3)bbcode_code_printable
• (1)bbcode_quote_printable
• (1)footer
• (1)gobutton
• (1)navbar_moderation
• (1)navbar_noticebit
• (2)option
• (1)spacer_close
• (1)spacer_open

Phrase Groups Available (3):
• global
• postbit
Included Files (19):
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_calendar.php
• ./includes/class_bbcode_alt.php
• ./includes/class_bbcode.php
• ./includes/functions_bigthree.php
• ./includes/functions_notice.php

Hooks Called (41):
• init_startup
• init_startup_session_setup_start
• database_pre_fetch_array
• database_post_fetch_array
• init_startup_session_setup_complete
• global_bootstrap_init_start
• global_bootstrap_init_complete
• cache_permissions
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• bbcode_fetch_tags
• bbcode_create
• bbcode_parse_start
• bbcode_parse_complete_precache
• bbcode_parse_complete
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates