Spreadsheet Basic (spBasic) - Random Numbers
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 


Random Number Example (Screen Shot)

 

This screen shot shows a chart of random numbers (Y) with an average computed from all previous values (Average), and a running average computed from the last 'numRunningAvg' points (numRunningAvg = 6 in the example). The spBasic program below is used to generate the chart from information provided by the user in cells b3 and b4. The program uses the Excel RAND and AVERAGE functions.

The screen shot below shows the spBasic Edit Form for the 'Random' program.

 

You can copy the listing below and insert it into your Edit Form. Insert the variables into a worksheet column. This program is also included in the 'C:\Program Files\spBasic\Examples\' directory.

NOTE: Install the Analysis Toolpak AddIn to use the advanced Excel functions.

// variables
MaxValue
numRunningAvg
i
End

// ----- Random -----
// NOTE: anything after the characters "//" are comments and
//             are ignored by the program
//     MaxValue and numRunningAvg  are set by the user
clear c2:f100                                                         //clear the data cells
// -- loop from 2 to 71 --
for i = 2 to 71
  c<<i>> = i - 1
  d<<i>>=maxvalue*rand()                                    // random number
  // -- compute the average from the beginning cell --
  e<<i>> = fixed(Average(d2:d<<i>>),4,true)        // round to 4 places
  // -- compute a average using only the last 'numRunningAvg' cells --
  if (i - (numRunningAvg - 1)) >= 2 then
    f<<i>> = fixed(Average(d<<i - (numRunningAvg - 1)>>:d<<i>>),4,true)
  else
    f<<i>> = e<<i>>
  endif
next

End

 

 

 

 

 

 



   Copyright © 2007-2010,  Reportdata.com                                                                                                       Contact Us