Homework on JavaScript

Here is a series of assignments that allow you to use what you learned at the JavaScript session. At the session, the question came up "how good is the random number generator in JavaScript?" These assignments will assess two properties of the random number generator: First, is it equally likely that Math.random() is greater than or less than 1/2? Second, if we take two random numbers in succession, are these two independent?

1. Create a Web page and JavaScript program that will draw 1000 instances of a random number, x = Math.random(), and count how many instances are greater than 1/2.
Hints: Use loops, use if ( ) { }, and use document.write() to display the results.

2. Now expand your page and program to find two instances of the random number, x = Math.random() and y = Math.random() and count the frequencies of the four cases:

(a) x < 1/2 and y < 1/2
(b) x < 1/2 and y >= 1/2
(c) x >= 1/2 and y < 1/2
(d) x >= 1/2 and y >= 1/2

Hints: name the four frequencies A, B, C, and D, respectively. Start them at zero and use if ( ) { } else if ( ) { } ....to increment them. Use document.write() to display the four frequencies.

NOTE: As was pointed out to me at the Saturday session, the current JavaScript standard uses && for "and" and || for "or". See http://www.w3schools.com/js/js_comparisons.asp.

3. Now improve your program to allow any number of trials. Let n = the number of trials (iterations). Create a 3 X 3 table in which the first row and column are used to label the rows and columns, and the lower right 2 X 2 table contains four text input boxes in which the four frequencies will be displayed.
Hints: You will need a form. Name the form, panel, and name the four text boxes, A, B, C, and D, so that you can pass the results of the javaScript program to these four text boxes. Add a function, myCount(), that is called when the body loads to do the computations and put the results in the boxes.
Option: For Assignment 3a, you could also put the (x, y) pairs into a textarea, so that someone could copy them and paste them into Excel, which would allow drawing a scatterplot of the points. There should be no correlation between x and y, and the joint distribution should form a scatterplot that looks uniform on the rectangle.

4. Now expand your program so that it computes the Chi-Square of fit of the observed frequencies, and add an alert message that announces the Chi-Square. Also, add a prompt so that the user can specify the number of trials.
Hints: Use predicted frequencies of p = n/4 for each cell. Each term in the chi-square is the squared discrepancy between observed frequency and obtained frequency divided by the predicted frequency. The chi-square is the sum of the four terms. This chi-square will have 3 df because you do NOT estimate the probabilities from the row and col sum as is usually done, which would have yielded a chi-square with 1 df. Try your program several times: Are the results compatible with randomness? Your prompt should appear at the start of the function.

5. Now expand your program so that it calculates the time from the start of computations until the chi-square is calculated, and announce this time result as well as the chi-square in the alert box. How long does your program take to do 1,000,000 samples? It should require less than half a second.
Hints: start the timer after the user selects the number of trials. Finish the timer after chisq is calculated. If you print "\n" it will produce a line return inside the alert box.

6. Now add ANOTHER form that asks the user to judge the goodness of the random number generator using a text box with 80 characters to get a comment--or use another prompt to solicit this answer, if you like. You can use surveyWiz, if you like, to create this form. This form should have an ACTION that sends the results to the data file on ati-birnbaum.netfirms.com. That is, you can use the same ACTION that would be inserted by surveyWiz at that site. However, instead of a submit button, create a generic button that will call a function that transfers the chi-square from panel form to the data form, to be saved along with the user's judgment of the goodness of the "random" results.
Hint: You will need to add a NAME to the data form, allowing you to pass the data from form named panel to the data form. You will also need to add "hidden" variables to the data form to provide space for the results to be transferred.

7. For extra credit: Expand your program(s) so that it (they) can assess results for other values of p besides 1/2. Then expand it to assess mutual independence of three instances of Math.random(). If you run the program thousands of times, does the distribution of chi-squares matching the Chi-Square distribution? Compare Math.random() with results from random number generators provided by others that are available via the WWW:
http://baagoe.com/en/RandomMusings/javascript/

8. Extra credit: Expand your program to simulate the True and Error model of variability of choice, with two choice problems and two repetitions of the problems per person. Let n = the number of people (or in individual TE theory, the number of blocks). Set it up so that the user can specify the four true probabilities of the four response patterns, let e1 and e2 represent the error probabilities for the two choices. Use the trick in Exercise 3a to organize the hypothetical data in a text area.

This series of exercises uses almost all of the tricks that were taught at the session. I will provide solutions, over time, so you can see how to do these tasks. Solutions will be linked below:

Solutions to the Exercises

  1. Solution to Exercise 1
  2. Solution to Exercise 2
  3. Solution to Exercise 3
  4.    Solution to Exercise 3a
  5. Solution to Exercise 4
  6. Solution to Exercise 5
  7. Solution to Exercise 6