CODINGLISTINGS ★ Random reflections ★

Random Numbers (Computing with the Amstrad)Coding Listings
★ Ce texte vous est présenté dans sa version originale ★ 
 ★ This text is presented to you in its original version ★ 
 ★ Este texto se presenta en su versión original ★ 
 ★ Dieser Text wird in seiner Originalfassung präsentiert ★ 

JUST how random are the Amstrad's random numbers? Suppose the RND function had 100 goes at picking between 1, 2, 3 and 4. If they were equally likely you'd expect 25 occurrences of each. However run this month's program and you might change your ideas.

  • 10 Labels the program.
  • 20 Calls the subroutine that sets the initial conditions for the program to run.
  • 30-50 Form a FOR ... NEXT loop that cycles trials times.
  • 40 Sends the program off to the subroutine that produces and counts the random numbers. This routine will be called trials times.
  • 60 Draws the bar chart that summarises the results once the loop has finished.
  • 99 Stops the program crashing into the subroutine definitions. Leave this out and see what happens.
  • 100-199 Form the initialisation routine. All four running totals - used to count the number of times 1, 2, 3 and 4 are thrown up by the random number generator - are set to zero. This isn't strictly necessary but it is good practice.
  • 110 Scale is used to decide how long each bar in the bar chart will be. Initially six graphics coordinate points are used for every one occurrence of a number.
  • 160 Trials, used to decide the number of random numbers that the FOR... NEXT loop will generate, is set to 100.
  • 200-299 Generate a random number and keep a tally of each of the four possibilities.
  • 210 The expression produces a whole number in the range 1 to 4 and stores it in the temporary numeric variable number.
  • 220 Has number running the gauntlet of three nested IF ... THEN ... ELSE statements. Depending on the value of number the relevant total is increased by one.
  • 300-399 Is performed after the program drops out of the main FOR ... NEXT loop. By this time the four total variables will contain the tallies for the number of occurrences of 1, 2, 3 and 4 in 100 trials. Three further subroutines are called to display these results.
  • 400-499 Draw the two axes. Notice that the vertical axis is 300 (350-50) graphics coordinates in length.
  • 500-599 Create the bars, one for 1, 2, 3 and 4 respectively. The length of each bar is directly proportional to the number of
    times that that particular number occurs.
  • 510 Moves the graphics cursor to the point on the horizontal axis where the 1 bar will start.
  • 520 A vertical line is drawn, its length representing the number of 1s generated in trials loops. The Y coordinate is made up of an offset of 50 - to start the line on the axis -added to the number of 1s multiplied by the scaling factor.
  • It's just possible that there will be
  • 100 1s, and if this is the case the line would be 600 graphics coordinates long and go off the screen. As it is, the vertical axis will only cover 50 (300/6) occurrences but this is nearly always enough. I leave it to the statistics freaks to figure out the chances of the scale being exceeded.
  • 530-580 Move along the horizontal axis producing the lines for the other three numbers.
  • 600-699 Number the axes.
  • 605 Joins the text and graphics cursors. In effect you can use MOVE to decide where to print.
  • 610-640 Form a FOR ... NEXT loop which moves along below the horizontal axis, labelling each bar.
  • 620 In the expression the 90 is the distance apart of the bars, to which an offset is added to start the count at the origin -100,50. This offset is 84, not 100 as might be expected. Try 100 and you'll see that the figures are slightly out of line.
  • 630 Prints the number at the graphics cursor. The semicolon is needed to suppress the usually automatic carriage return and line feed. Leave it out and you'll see them being treated as graphics control codes with weird results.
  • 650-660 Label the highest value of the vertical axis. As the value of scale is changed the number that appears is automatically changed to the correct maximum value.
  • 670 Undoes the TAGging.

Things to do: Why not try different values of scale and trials, such as 1 and 600 or even 1 and 1000? Also why not join the tops of the bars with a line, so making a frequency polygon? Come to think of it, the bars are a bit thin. Can you make them thicker? Maybe windows would help. Finally how about a bit of colour?

CWTA

★ PUBLISHER: Computing with the Amstrad
★ YEAR: 1986
★ CONFIG: ???
★ LiCENCE: LISTING
★ COLLECTION: COMPUTING WITH THE AMSTRAD 1986
★ AUTHOR: Trevor Roberts
 

★ AMSTRAD CPC ★ DOWNLOAD ★

Type-in/Listing:
» Random  Numbers    (Computing  with  the  Amstrad)    ENGLISHDATE: 2020-07-23
DL: 183
TYPE: ZIP
SiZE: 4Ko
NOTE: 40 Cyls
.HFE: Χ

★ AMSTRAD CPC ★ A voir aussi sur CPCrulez , les sujets suivants pourront vous intéresser...

Lien(s):
» Coding Src's » Graphic - Random Symmetrical Pattern Generators
» Coding Src's » Random Lines
» Coding Src's » Random Numbers Generator ( 2 src's )
» Coding Src's » Truly Random RND (CPC Computing)
» Coding Src's » Random Access Filing on the CPC (Computing with the Amstrad)
» Coding Src's » Random Lissajoux Figures
Je participe au site:
» Vous avez des infos personnel, des fichiers que nous ne possédons pas concernent ce programme ?
» Vous avez remarqué une erreur dans ce texte ?
» Aidez-nous à améliorer cette page : en nous contactant via le forum ou par email.

CPCrulez[Content Management System] v8.7-desktop/c
Page créée en 653 millisecondes et consultée 1662 fois

L'Amstrad CPC est une machine 8 bits à base d'un Z80 à 4MHz. Le premier de la gamme fut le CPC 464 en 1984, équipé d'un lecteur de cassettes intégré il se plaçait en concurrent  du Commodore C64 beaucoup plus compliqué à utiliser et plus cher. Ce fut un réel succès et sorti cette même années le CPC 664 équipé d'un lecteur de disquettes trois pouces intégré. Sa vie fut de courte durée puisqu'en 1985 il fut remplacé par le CPC 6128 qui était plus compact, plus soigné et surtout qui avait 128Ko de RAM au lieu de 64Ko.