Code archives/Algorithms/Pecentage values

This code has been declared by its author to be Public Domain code.

Download source code

Pecentage values by BitmaniaK
(Posted 25 years ago)
Here some functions for AI & Statistics
Percentage ratio
controlled percentage random,
fuzzy (0..1) percentage random

Test the demo
Gianluca (aka Bitmaniak)
;******************************************************
;*** This function returns a random number
;*** with percentage parameter
;*** percRnd returns a float value
;*** percRand returns an integer value
;***
;*** Example.1 : percRnd(25) 
;***	 Return 1 with 25% of probability
;***     Return 0 with 75% (100-25) of probability
;***
;*** Example.2 : percRnd(25,1000) 
;***	 Return 1 with 2.5% of probability
;***     Return 0 with 97.5% (1000-25) of probability
;******************************************************
Function percRnd#(random#, max#=100)
	Return random=>Rnd(max)
End Function

Function percRand(random, max=100)
	Return random=>Rand(max)
End Function
;******************************************************
;*** Return the percentual of value to max
;*** Example.1: percent(25,100) = 25.000
;*** Example.2: percent(25,1000) = 2.500
;******************************************************
Function percent#(value#,max#)
	Return value* 100.0 / max#
End Function

;******************************************************
;*** This function returns a random number
;*** of a percentage parameter from 0 to 1
;***
;*** Example.1 : percFuzzy(25) 
;***	 0 > Return >=1 with 25% of probability
;***     Return 0 with 75% (100-25) of probability
;******************************************************
Function fuzzyRnd#(random#, max#=100)
	r# = Rnd(max)
	If random=>r
		Return Float(r/random)
	End If
	Return 0
End Function
;******************************************************

Print "tests (eg.1000, bigger = better precision)"
test# = Input(">")
Print "percent (eg.10, max = 100)"
perc# = Input(">")

Print 
SeedRnd MilliSecs() 
tot = 0
fuz = 0
;*** perfoms a lot of tests
For t=1 To test
	If percRnd(perc) tot=tot+1
	If fuzzyRnd(perc)<>0 fuz=fuz+1
Next
;*** report statistics
Print
Print &quot;Test     : &quot; 
Print tot  + &quot;/&quot; + test + &quot;=&quot; + Float(test/tot) +&quot; &quot;+ percent(tot,test) + &quot;%&quot;
Print &quot;Attended : &quot; 
Print percent(perc,100) + &quot;%&quot;
Print &quot;Error: &quot; 
Print Abs(percent(tot,test) - perc)
Print
Print &quot;Fuzzies  : &quot; 
Print fuz  + &quot;/&quot; + test + &quot;=&quot; + Float(test/fuz) +&quot; &quot;+ percent(fuz,test) + &quot;%&quot;
Print &quot;Attended : &quot; 
Print percent(perc,100) + &quot;%&quot;
Print &quot;Error: &quot; 
Print Abs(percent(fuz,test) - perc)
Print
Print &quot;percRnd(25) = &quot; + percRnd(25)
Print &quot;percRnd(25,1000) = &quot; + percRnd(25,1000)
Print &quot;percent(25,100) = &quot; +percent(25,100)
Print &quot;percent(25,1000) = &quot; + percent(25,1000)
Print