December 04, 2020, 11:19:25 AM

Author Topic: [bb] Pecentage values by Giano [ 1+ years ago ]  (Read 387 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Pecentage values by Giano [ 1+ years ago ]
« on: June 29, 2017, 12:28:42 AM »
Title : Pecentage values
Author : Giano
Posted : 1+ years ago

Description : Here some functions for AI & Statistics
Percentage ratio
controlled percentage random,
fuzzy (0..1) percentage random

Test the demo
Gianluca (aka Bitmaniak)


Code :
Code: BlitzBasic
  1. ;******************************************************
  2. ;*** This function returns a random number
  3. ;*** with percentage parameter
  4. ;*** percRnd returns a float value
  5. ;*** percRand returns an integer value
  6. ;***
  7. ;*** Example.1 : percRnd(25)
  8. ;***     Return 1 with 25% of probability
  9. ;***     Return 0 with 75% (100-25) of probability
  10. ;***
  11. ;*** Example.2 : percRnd(25,1000)
  12. ;***     Return 1 with 2.5% of probability
  13. ;***     Return 0 with 97.5% (1000-25) of probability
  14. ;******************************************************
  15. Function percRnd#(random#, max#=100)
  16.         Return random=>Rnd(max)
  17. End Function
  18.  
  19. Function percRand(random, max=100)
  20.         Return random=>Rand(max)
  21. End Function
  22. ;******************************************************
  23. ;*** Return the percentual of value to max
  24. ;*** Example.1: percent(25,100) = 25.000
  25. ;*** Example.2: percent(25,1000) = 2.500
  26. ;******************************************************
  27. Function percent#(value#,max#)
  28.         Return value* 100.0 / max#
  29. End Function
  30.  
  31. ;******************************************************
  32. ;*** This function returns a random number
  33. ;*** of a percentage parameter from 0 to 1
  34. ;***
  35. ;*** Example.1 : percFuzzy(25)
  36. ;***     0 > Return >=1 with 25% of probability
  37. ;***     Return 0 with 75% (100-25) of probability
  38. ;******************************************************
  39. Function fuzzyRnd#(random#, max#=100)
  40.         r# = Rnd(max)
  41.         If random=>r
  42.                 Return Float(r/random)
  43.         End If
  44.         Return 0
  45. End Function
  46. ;******************************************************
  47.  
  48. Print "tests (eg.1000, bigger = better precision)"
  49. test# = Input(">")
  50. Print "percent (eg.10, max = 100)"
  51. perc# = Input(">")
  52.  
  53. Print
  54. SeedRnd MilliSecs()
  55. tot = 0
  56. fuz = 0
  57. ;*** perfoms a lot of tests
  58. For t=1 To test
  59.         If percRnd(perc) tot=tot+1
  60.         If fuzzyRnd(perc)<>0 fuz=fuz+1
  61. Next
  62. ;*** report statistics
  63. Print
  64. Print "Test     : "
  65. Print tot  + "/" + test + "=" + Float(test/tot) +" "+ percent(tot,test) + "%"
  66. Print "Attended : "
  67. Print percent(perc,100) + "%"
  68. Print "Error: "
  69. Print Abs(percent(tot,test) - perc)
  70. Print
  71. Print "Fuzzies  : "
  72. Print fuz  + "/" + test + "=" + Float(test/fuz) +" "+ percent(fuz,test) + "%"
  73. Print "Attended : "
  74. Print percent(perc,100) + "%"
  75. Print "Error: "
  76. Print Abs(percent(fuz,test) - perc)
  77. Print
  78. Print "percRnd(25) = " + percRnd(25)
  79. Print "percRnd(25,1000) = " + percRnd(25,1000)
  80. Print "percent(25,100) = " +percent(25,100)
  81. Print "percent(25,1000) = " + percent(25,1000)
  82. Print


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal