Tcl Library Source Code

Check-in [233f6a741b]
Login
Bounty program for improvements to Tcl and certain Tcl packages.
Tcl 2019 Conference, Houston/TX, US, Nov 4-8
Send your abstracts to [email protected]
or submit via the online form by Sep 9.

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add procedures to generate random numbers that have a triangular distribution function (this is in response to ticket [cc77e167bf]).
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 233f6a741b7ebc31b47b76d692d284d954208355a49b791bff544b007316528f
User & Date: arjenmarkus 2019-06-24 18:49:27
Original Comment: Add procedures to generate random numbers that have a triangular distribution function (this is in response to ticket https://core.tcl-lang.org/tcllib/tktview?name=cc77e167bf)
References
2019-06-25
18:01 Ticket [cc77e167bf] package simulation::random lacks triangular distribution status still Open with 3 other changes artifact: 61ac99c2e7 user: aku
Context
2019-06-24
19:31
Integrated logger fixes for ticket [e4d5ef01e7]. check-in: 4955240acf user: aku tags: trunk
18:49
Add procedures to generate random numbers that have a triangular distribution function (this is in response to ticket [cc77e167bf]). check-in: 233f6a741b user: arjenmarkus tags: trunk
18:25
Integrated ncgi fixes for tkt [1f900bdf6b]. check-in: 2adb057376 user: aku tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to modules/simulation/pkgIndex.tcl.

1
2
3
package ifneeded simulation::random 0.3.1 [list source [file join $dir random.tcl]]
package ifneeded simulation::montecarlo 0.1 [list source [file join $dir montecarlo.tcl]]
package ifneeded simulation::annealing 0.2 [list source [file join $dir annealing.tcl]]
|


1
2
3
package ifneeded simulation::random 0.4.0 [list source [file join $dir random.tcl]]
package ifneeded simulation::montecarlo 0.1 [list source [file join $dir montecarlo.tcl]]
package ifneeded simulation::annealing 0.2 [list source [file join $dir annealing.tcl]]

Changes to modules/simulation/random.tcl.

428
429
430
431
432
433
434




































































435
436
437
438
439
440
441
442
443
444
         set z [expr {DEPTH*rand()}]
         return [list $x $y $z]
    }]

    return $name
}





































































# Announce the package
#
package provide simulation::random 0.3.1


# main --
#     Test code
#
if { 0 } {
set bin [::simulation::random::prng_Bernoulli 0.2]






>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


|







428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
         set z [expr {DEPTH*rand()}]
         return [list $x $y $z]
    }]

    return $name
}


# prng_Triangle --
#     Create a PRNG with a triangular distribution of points on an interval.
#     If the argument min is lower than the argument max, then smaller
#     values have higher probability and vice versa.
#
# Arguments:
#     min       Minimum value
#     max       Maximum value
#
# Result:
#     Name of a procedure that returns the random point
#
proc ::simulation::random::prng_Triangle {min max} {
    variable count

    incr count

    set name ::simulation::random::PRNG_$count

    set diff [expr {$max-$min}]

    if { $diff > 0.0 } {
        proc $name {} [string map [list MIN $min DIFF $diff] \
         {
            set r [expr {1.0 - sqrt(1.0 - rand())}]
            set x [expr {MIN + DIFF*$r}]
            return $x
        }]
    } else {
        proc $name {} [string map [list MAX $max DIFF $diff] \
         {
            set x [expr {MAX - DIFF*sqrt(rand())}]
            return $x
        }]
    }

    return $name
}


# prng_SymmetricTriangle --
#     Create a PRNG with a symmetric triangular distribution of points on an interval.
#
# Arguments:
#     min       Minimum value
#     max       Maximum value
#
# Result:
#     Name of a procedure that returns the random point
#
proc ::simulation::random::prng_SymmetricTriangle {min max} {
    variable count

    incr count

    set name ::simulation::random::PRNG_$count

    set diff2 [expr {0.5 *($max-$min)}]

    proc $name {} [string map [list MIN $min DIFF2 $diff2] \
     {
        return [expr {MIN + DIFF2 * (rand() + rand())}]
    }]

    return $name
}

# Announce the package
#
package provide simulation::random 0.4.0


# main --
#     Test code
#
if { 0 } {
set bin [::simulation::random::prng_Bernoulli 0.2]

Changes to modules/simulation/random.test.

230
231
232
233
234
235
236
237


































238
239
test "Block-1.0" "block generator with sides 10, 0.1 and 2.5" \
    -body {
    set p [::simulation::random::prng_Block 10 0.1 2.5]
    set rnd [$p]
    set result [llength $rnd]
} -result 3



































# End of test cases
testsuiteCleanup







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
test "Block-1.0" "block generator with sides 10, 0.1 and 2.5" \
    -body {
    set p [::simulation::random::prng_Block 10 0.1 2.5]
    set rnd [$p]
    set result [llength $rnd]
} -result 3

test "Triangle-1.0" "triangularly distributed numbers between -1.0 and 1.0" \
    -body {
    set p [::simulation::random::prng_Triangle -1.0 1.0]

    set okay 1
    for {set i 0} {$i < 1000} {incr i} {
        set rnd [$p]

        if { $rnd < -1.0 || $rnd > 1.0 } {
            set okay 0
            break
        }
    }

    set okay
} -result 1

test "Triangle-1.1" "triangularly distributed numbers between -1.0 and 1.0 (alternative)" \
    -body {
    set p [::simulation::random::prng_Triangle 1.0 -1.0]

    set okay 1
    for {set i 0} {$i < 1000} {incr i} {
        set rnd [$p]

        if { $rnd < -1.0 || $rnd > 1.0 } {
            set okay 0
            break
        }
    }

    set okay
} -result 1

# End of test cases
testsuiteCleanup

Changes to modules/simulation/simulation_random.man.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
..
77
78
79
80
81
82
83


























84
85
86
87
88
89
90
[comment {-*- tcl -*- doctools manpage}]
[manpage_begin simulation::random n 0.1]
[keywords math]
[keywords {random numbers}]
[keywords simulation]
[keywords {statistical distribution}]
[copyright {2004 Arjen Markus <[email protected]>}]
[moddesc   {Tcl Simulation Tools}]
[titledesc {Pseudo-random number generators}]
[category  Mathematics]
[require Tcl [opt 8.4]]
[require simulation::random 0.1]

[description]
[para]
This package consists of commands to generate pseudo-random number
generators. These new commands deliver

[list_begin itemized]
................................................................................
[list_begin definitions]

[call [cmd ::simulation::random::prng_Uniform] [arg min ] [arg max]]

Create a command (PRNG) that generates uniformly distributed numbers
between "min" and "max".



























[list_begin arguments]
[arg_def float min] Minimum number that will be generated
[arg_def float max] Maximum number that will be generated
[list_end]

[para]

|









|







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
..
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
[comment {-*- tcl -*- doctools manpage}]
[manpage_begin simulation::random n 0.4]
[keywords math]
[keywords {random numbers}]
[keywords simulation]
[keywords {statistical distribution}]
[copyright {2004 Arjen Markus <[email protected]>}]
[moddesc   {Tcl Simulation Tools}]
[titledesc {Pseudo-random number generators}]
[category  Mathematics]
[require Tcl [opt 8.4]]
[require simulation::random 0.4]

[description]
[para]
This package consists of commands to generate pseudo-random number
generators. These new commands deliver

[list_begin itemized]
................................................................................
[list_begin definitions]

[call [cmd ::simulation::random::prng_Uniform] [arg min ] [arg max]]

Create a command (PRNG) that generates uniformly distributed numbers
between "min" and "max".

[list_begin arguments]
[arg_def float min] Minimum number that will be generated
[arg_def float max] Maximum number that will be generated
[list_end]

[para]

[call [cmd ::simulation::random::prng_Triangular] [arg min ] [arg max]]

Create a command (PRNG) that generates triangularly distributed numbers
between "min" and "max". If the argument min is lower than the argument max, then smaller
values have higher probability and vice versa. In the first case the probability
density function is of the form [emph {f(x) = 2(1-x)}] and the other case it is of the form [emph {f(x) = 2x}].

[list_begin arguments]
[arg_def float min] Minimum number that will be generated
[arg_def float max] Maximum number that will be generated
[list_end]

[para]

[call [cmd ::simulation::random::prng_SymmTriangular] [arg min ] [arg max]]

Create a command (PRNG) that generates numbers distributed according to a symmetric triangle
around the mean of "min" and "max".

[list_begin arguments]
[arg_def float min] Minimum number that will be generated
[arg_def float max] Maximum number that will be generated
[list_end]

[para]