RANDPROB JavaScript (stand-alone) is a code insert that generates "n+1" random indexes used for selection of Go problem names from the predefined sectioned Go problem array representing Go problem selection list. RANDPROB.JS contains a sample of Go Problem name array definition and code for random indexes selection.
RANDPROB JavaScript is a stand-alone
one, which means that it s kept in a separate RANDPROB.JS file that gets
included into HTML <BODY> (normally at its start and always
after the the include defining the Go Problem array) using the following
operator (directory levelling may vary):
<SCRIPT LANGUAGE=JavaScript SRC=PROBLEMS.JS></SCRIPT>
<SCRIPT LANGUAGE=JavaScript SRC=RANDPROB.JS></SCRIPT>
Included JavaScript code is executed in place to generate random indexes that
are used for Go Problem selection in the HTML code below JavaScript insert.
Note: |
Before reading further you might want to look at explanation of the expression used to generate an integer random number within the specified range. |
<!-- Generate Indexes for Random Go Problem Selection 01/08/2001–01/24/2001 --> <!-- ------------------------------------------------------------ 01/24/2001 --> <!-- www.davar.net/GO/PROBLEMS/RANDPROB.JS --> <!-- Copyright (C) 2001 by Vladimir Veytsel --> <!-- // RANDPROB is a code insert that is used to generate random indexes for // selection of problem names from sectioned problem list. // Code presumes that the array of problem names is declared and initialized. // The variables that split problem list into sections should have their // values assigned as in the example below: // P=new Array(14) // Problem array (dynamically sized; starts from "0") // S=new Array( 7) // Section array (dynamically sized; starts from "0") // S[ 0]=0 // Used for uniform treatment of section ranges // P[ 1]="TSJBOW01" // S[ 1]=1 // End of 1-st section (Break Out) // P[ 2]="TSJCCT01" // S[ 2]=2 // End of 2-nd section (Capture Cutting Stone) // P[ 3]="TSJCLB01" // P[ 4]="TSJCLH01" // P[ 5]="TSJCLH02" // P[ 6]="TSJCLK01" // P[ 7]="TSJCLK02" // P[ 8]="TSJCLS01" // S[ 3]=8 // End of 3-rd section (Capture to Live) // P[ 9]="TSJCST01" // S[ 4]=9 // End of 4-th section (Capture Stones) // P[10]="TSJCNT01" // S[ 5]=10 // End of 5-th section (Connect Stones) // P[11]="TSJMSS01" // P[12]="TSJMSS02" // P[13]="TSJMSS03" // As a result of the execution of the code below the random indexes will be // generated to be used within HTML text for random problem selection (using // "document.write"): // i[0] Random index within entire list that differs from all section indexes // i[n] Random index within n-th section // Warning: If the total number of problems in the problem list is equal // to the number of sections, index "i[0]" will be equal to one // of the section indexes. i=new Array(S.length) // Index array (dynamically sized; starts from "0") S[S.length-1]=P.length-1 // End of last section Total=P.length-1 i[0]=Math.floor(Math.random()*Total)%Total+1 for (n=1;n<S.length;n++) i[n]=(i[0]%(S[n]-S[n-1]))+S[n-1]+1 // Random index within n-th section // Random index within the entire list (differs from all section indexes) if (Total>S.length-1) // Prevent infinite cycle {Valid=false // Index valid flag while (!Valid) // Repeat until valid index is generated {i[0]=Math.floor(Math.random()*Total)%Total+1 for (n=1;n<S.length;n++) {Valid=(i[0]!=i[n]) if (!Valid) break } } } //-->
RANDPROB JavaScript is a generalization of a similar RANDSONG.JS JavaScript, which performs the same task of random selection, but operates on a list divided into exactly 3 sections — a typical special case used for Russian song lists at this web site (see sample song list).