<!-- Generate Indexes for Random Song Selection  07/27/1999-01/24/2001 -->
<!-- ------------------------------------------------------ 01/24/2001 -->
<!-- www.davar.net/RUSSIAN/SONGS/RANDSONG.JS                           -->
<!-- Copyright (C) 1999-2001 by Vladimir Veytsel                       -->
<!--

// RANDSONG is a code insert that is used to generate random indexes for
// selection of song names from sectioned song list.

// Code presumes that the array of song names is declared and initialized.
// Two variables that split song list into three sections should have their
// values assigned as in the example below:

// S=new Array(9)  // Song array (starts from "0")

// S[1]="Name_1"
// S[2]="Name_2"
// S[3]="Name_3"; Sect_1=3  // End of 1-st section

// S[4]="Name_4"
// S[5]="Name_5"
// S[6]="Name_6"
// S[7]="Name_7"; Sect_2=7  // End of 2-nd section

// S[8]="Name_8"
// S[9]="Name_9"

// As a result of the execution of the code below four random indexes will be
// generated to be used within HTML text for random song selection (using
// "document.write"):

// j0  Random index within entire list that differs from all 3 section indexes
// i1  Random index within 1-st section
// i2  Random index within 2-nd section
// i3  Random index within 3-rd section

// The exhaustive example of section indexes calculation from a random index
// within the entire song array:

// i0  1 2 3 4 5 6 7 8 9  Modulo  Incr  Range
// --  - - - - - - - - -  ------  ----  -----
// i1  2 3 1 2 3 1 2 3 1  % 3       +1  [1-3]
// i2  5 6 7 4 5 6 7 4 5  %(7-3)  +3+1  [4-7]
// i3  9 8 9 8 9 8 9 8 9  %(9-7)  +7+1  [8-9]
//                          | |    |
//                          | |    Last index of PREVIOUS section
//                          | Last index of PREVIOUS section
//                          Last index of CURRENT section

// Warning: If the total number of names in the song list is three,
//          index "i0" will be equal to one of the section indexes.

   Sect_3=S.length-1  // End of 3-rd section and entire list

   i0=Math.floor(Math.random()*Sect_3)%Sect_3+1

   i1=(i0% Sect_1)+1                 // Random index within 1-st section
   i2=(i0%(Sect_2-Sect_1))+Sect_1+1  // Random index within 2-nd section
   i3=(i0%(Sect_3-Sect_2))+Sect_2+1  // Random index within 3-rd section

// Random index within the entire list (differs from all 3 section indexes)
   if (Sect_3>3)       // Prevent infinite cycle
      {Valid=false     // Index valid flag
       while (!Valid)  // Repeat until valid index is generated
             {i0=Math.floor(Math.random()*Sect_3)%Sect_3+1
              Valid=(i0!=i1)&&(i0!=i2)&&(i0!=i3)
             }
      }

//-->

