2018年3月1日 星期四

Random Number Application in System Testing


Random Number Application in System Testing


Ching-Tang Tseng
Hamilton, New Zealand
2 March 2018
ilikeforth@gmail.com

The best and fast method to generate mass data for testing is using random number. At the end of mathematic system development must to do a lot of testing, especially for all data structures. Recently, I added my ABC FORTH to a Lina64 bare Forth system already. Testing is in progress. This article shows you how to use random number to test my system?

First, I have to explain a few special data formats and some special usages of words for this system. I must emphasize also: Many aspects in my system violate current so called FORTH Standard totally. There are a pure software floating point system and an extended complex number system to be included in my system. Besides, both of them are able to be used in BASIC style programming condition as well.

Only two kinds of number format can be accepted in this system. One is the general integer number without decimal point. Another one is the general double integer number with a decimal point. Then, a floating point number must be expressed as 1.2345 E -3 such a format, E is separated from the front double number and E is separated from the following integer also. This insistence has its advantage. It simplified a lot converting treatment for the input of number. Therefore, the output format of a floating point number must be the same as an input format. A complex number is composed by two floating point number. Its expressive format follows this regulation as well.

In Forth, 'i' is a primitive word. In BASIC style program, 'i' is always to be used as an index. In complex number expression, 'i' is a necessary mark. They are disturbing each other when you are using them. For the sake of simplification, all 'i' in the complex number are replaced by 'ii'. “ ii “ is not only to be used as a complex number output mark, but also is a high level defined word while data input.

All random numbers are coming out from the unique integer random number generator. I am not going to discuss its generating mechanism here. Let us just suppose it works well.

Integer random number generator kernel word named RANDOM. It supports its following application word named RAND. Their stack description is expressed as following:
RANDOM ( -- n )
RAND ( n1 – n2 )
The same, floating point random number and complex random number should be:
FRANDOM ( -- r )
FRAND ( r1 – r2 )
ZRANDOM ( -- z )
ZRAND ( z1 – z2 )

n1, r1, z1 are upper bound of generating range in each kind of random number. In INTEGER mode condition, only positive random number will be generated. To get a negative integer random number need to do a NEGATE operation.

In BASIC environment, three random number functions are using the same name: RND. It is impossible to be confused by each other when you are using RND in each environment.
In general, to do RANDOMIZE once before the random number to be used can improved the random effect. Based on the fast changing content in a hardware counter beside CPU, this instruction was designed.

Many testing had been done on all BASIC style data structures. They were:

In Integer: INTEGER, INTEGERS, (ARRAY), (MATRIX), (TENSOR)
In Floating point: REAL, REALS, ARRAY, MATRIX, TENSOR
In complex: COMPLEX, COMPLEXS (gave such a name on purpose), [ARRAY], [MATRIX], [TENSOR]

I am unable to list out all testing results with one article. I select the most complicated data structure of a complex tensor: [TENSOR] then to do data accessing. Please pay attention to all index 0 parts. Such a data structures let all index operation can be started from 0. Memory saving requirement are not a worth concerned problem.

There were no topics about execution speed or precision to be discussed in this article. All sub-systems were designed for personal using only. Let the last achievement apt to reuse on a traditional Forth system even after 50 years is my main goal. And system ability should be useful in solving all mathematical calculation problems. I am certainly sure, base on Lina64 Forth system, hundreds or even thousands digits precision floating point sub-system could be implemented out in the near future. I am not in a hurry to do it.

When you are looking at BASIC style code, please pay more attention on the mathematical expressed format, and try to feel:

How to let the system can do solving on it?

Many people don’t like BASIC style code. It doesn’t matter in my ABC FORTH. You are able to write your code in FORTH style still. I didn’t deleted anything belongs to original FORTH base system. So there are some mixing code of FORTH and BASIC to be arranged in the definition of MAIN on purpose.

These codes and its execution result record video are showing as following:


\ random number test

\ beware! In integer mode, need a NEGATE operation to get negative random number
: NTEST 10 0 DO CR 10 0 DO 100 RAND NEGATE 4 .R LOOP LOOP ;
: FTEST 10 0 DO CR -1.0 E 2 FRAND F. LOOP ;
\ beware! In FORTH style code, upper bound input format ignore + and ii
: ZTEST 10 0 DO CR -1.0 E 2 1.0 E 1 ZRAND Z. LOOP ;

3 integers i j k
complex z0
3 3 3 [tensor] zzz

: init
basic
10 let z{ z0 = 0.0 E 0 + 0.0 E 0 ii }z
20 for i = 1 to 3
30 for j = 1 to 3
40 for k = 1 to 3
\ attention! this is the usage to create a complex random number
50 let z{ zzz ( i j k ) = RND ( 1.0 E 2 - 1.0 E 1 ii ) }z
60 next k
70 next j
80 next i
90 end ;

: MAIN
randomize
ntest cr ftest cr ztest cr
init
basic
10 for i = 0 to 3
20 for j = 0 to 3
30 for k = 0 to 3
40 print " zzz( " ; i ; j ; k ; " )= " ; z{ zzz ( i j k ) }z
50 let z{ z0 = z0 + zzz ( i j k ) }z
60 next k
70 next j
80 next i
90 print cr cr " z0 = " ; z{ z0 }z
100 end
cr ." --- The end of testing --- "
;



My personal testing remarks: all random number generator functions approved

沒有留言: