Go to:  Davar site entry | Site contents | Site index | Internet | JavaScript | Text bottom

Index  Generation  JavaScript  for  Random  Song  Selection

RANDSONG JavaScript (stand-alone) is a code insert that generates 4 random indexes used for selection of song names from the predefined sectioned song array representing song play list.  RANDSONG.JS contains a sample of song name array definition and code for random indexes selection.

RANDSONG JavaScript is a stand-alone one, which means that it s kept in a separate RANDSONG.JS file that gets included into HTML <BODY> (normally at its start and always after the the include defining the song array) using the following operator (directory levelling may vary):


       <SCRIPT LANGUAGE=JavaScript SRC=MIRZAYAN.JS></SCRIPT>
       <SCRIPT LANGUAGE=JavaScript SRC=../RANDSONG.JS></SCRIPT>
     
Included JavaScript code is executed in place to generate random indexes that are used for song 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 Song Selection  07/27/199901/24/2001 -->
<!-- ------------------------------------------------------ 01/24/2001 -->
<!-- www.davar.net/RUSSIAN/SONGS/RANDSONG.JS                           -->
<!-- Copyright (C) 19992001 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 (dynamically sized; 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  [13]
// i2  5 6 7 4 5 6 7 4 5  %(73)  +3+1  [47]
// i3  9 8 9 8 9 8 9 8 9  %(97)  +7+1  [89]
//                          | |    |
//                          | |    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)
             }
      }

//-->
  

RANDSONG JavaScript is a special case of a similar generalized RANDPROB.JS JavaScript, which performs the same task of random selection, but operates on a list divided into into any number of sections a universal approach used for Go Problems list at this web site.


View Sample Song List       View [and save] RANDSONG.TXT text
(Use [Back] button or [Alt]+[CL] to return here from the viewed page/text)
To make text executable rename it to *.JS and make a global change of "&lt;" into "<" signs.
Copyright © 19992001 by
Go to:  Davar site entry | Site contents | Site index | Internet | JavaScript | Text top