├── .gitignore ├── .travis.yml ├── LICENSE ├── test └── test.v ├── README.md └── vconrand.v /.gitignore: -------------------------------------------------------------------------------- 1 | test/test 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: minimal 2 | 3 | before_install: 4 | # move module files to module directory 5 | - mkdir vconrand 6 | - find . -maxdepth 1 -type f -print0 | xargs -0 mv -t vconrand 7 | # download and compile v 8 | - wget https://github.com/vlang/v/archive/master.zip 9 | - unzip master.zip 10 | - cd v-master 11 | - make 12 | - cd .. 13 | 14 | script: 15 | # build module 16 | - v-master/v build module $PWD/vconrand/vconrand.v 17 | # run test scripts 18 | - v-master/v run test/test.v 19 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Clemens Schmid 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /test/test.v: -------------------------------------------------------------------------------- 1 | import vconrand 2 | import rand 3 | import time 4 | import math 5 | 6 | fn main() { 7 | 8 | rand.seed(time.now().uni) 9 | 10 | // unif 11 | println('vconrand.runif(384.12, 399.54) = ' + 12 | vconrand.runif(384.12, 399.54).str() 13 | ) 14 | println('vconrand.runif_n(5, 384.12, 399.54) = ' + 15 | vconrand.runif_n(5, 384.12, 399.54).str() 16 | ) 17 | 18 | // norm 19 | println('vconrand.rnorm(14.81, 2.73) = ' + 20 | vconrand.rnorm(14.81, 2.73).str() 21 | ) 22 | println('vconrand.rnorm_n(5, 14.81, 2.73) = ' + 23 | vconrand.rnorm_n(5, 14.81, 2.73).str() 24 | ) 25 | 26 | // arbitrary 27 | mut arr := [0].repeat(200) 28 | for i := 0; i < 200; i++ { 29 | arr[i] = i 30 | } 31 | mut freq := [0].repeat(200) 32 | for i := 0; i < 200; i++ { 33 | freq[i] = distribution_function(i) 34 | } 35 | println('vconrand.rarb_int(arr, freq) = ' + 36 | vconrand.rarb_int(arr, freq).str() 37 | ) 38 | println('vconrand.rarb_int_n(5, arr, freq) = ' + 39 | vconrand.rarb_int_n(5, arr, freq).str() 40 | ) 41 | 42 | // sample 43 | arr2 := [0].repeat(10) 44 | println(vconrand.sample(arr2, 5).str()) 45 | arr3 := ["test"].repeat(10) 46 | println(vconrand.sample(arr3, 5).str()) 47 | 48 | } 49 | 50 | fn distribution_function(i int) int { 51 | x := f64(i) 52 | res := 10.0 * math.sin(0.1 * (x - 0.1)) + 10.0 53 | return int(math.round(res)) 54 | } 55 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | :exclamation: **This package is not up to date. Please use [Delta456/random](https://github.com/Delta456/random)** 2 | 3 | *** 4 | 5 | [![Build Status](https://www.travis-ci.org/nevrome/vconrand.svg?branch=master)](https://www.travis-ci.org/nevrome/vconrand) 6 | 7 | # vconrand: Convenient random number generators for V 8 | A collection of useful, simple random number generators and sampling methods. I wrote this to make it more easy to translate R simulation code to V. 9 | 10 | ## Installation 11 | To install vconrand, you can use [VPM](https://github.com/yue-best-practices/vpm). 12 | 13 | ```bash 14 | $ vpm init 15 | $ vpm get https://github.com/nevrome/vconrand vconrand 16 | ``` 17 | 18 | ## Quickstart 19 | 20 | ```v 21 | import vconrand 22 | import rand 23 | import time 24 | import math 25 | 26 | fn main() { 27 | 28 | rand.seed(time.now().uni) 29 | 30 | // random number from a uniform distribution 31 | vconrand.runif(384.12, 399.54) 32 | vconrand.runif_n(5, 384.12, 399.54) 33 | 34 | // random number from a normal distribution 35 | vconrand.rnorm(14.81, 2.73) 36 | vconrand.rnorm_n(5, 14.81, 2.73) 37 | 38 | // random integer from an arbitrary distribution 39 | mut arr := [0].repeat(200) 40 | for i := 0; i < 200; i++ { 41 | arr[i] = i 42 | } 43 | mut freq := [0].repeat(200) 44 | for i := 0; i < 200; i++ { 45 | freq[i] = distribution_function(i) 46 | } 47 | vconrand.rarb_int(arr, freq).str() 48 | vconrand.rarb_int_n(5, arr, freq).str() 49 | 50 | } 51 | 52 | fn distribution_function(i int) int { 53 | x := f64(i) 54 | res := 10.0 * math.sin(0.1 * (x - 0.1)) + 10.0 55 | return int(math.round(res)) 56 | } 57 | ``` 58 | 59 | See `test/test.v` for some code that shows every function. 60 | -------------------------------------------------------------------------------- /vconrand.v: -------------------------------------------------------------------------------- 1 | module vconrand 2 | 3 | import rand 4 | import math 5 | 6 | /** 7 | * ########################### 8 | * ########## runif ########## 9 | * ########################### 10 | */ 11 | 12 | /** 13 | * runif - random number generator with an uniform distribution 14 | * 15 | * The implemented solution most likely does not cover the full range 16 | * of precision of f64, because it is limited to INT_MAX possible 17 | * values. 18 | * 19 | * @param min lower limit of the distribution 20 | * @param max upper limit of the distribution 21 | * 22 | * @return a random value between min and max 23 | */ 24 | pub fn runif(min f64, max f64) f64 { 25 | 26 | range := max - min 27 | div := f64(C.RAND_MAX) / range 28 | res := min + (f64(rand.next(C.RAND_MAX)) / div) 29 | 30 | return res 31 | } 32 | 33 | /** 34 | * runif_n - runif but for an array of random values 35 | * 36 | * Uses runif and therefore has the same precision issue. 37 | * 38 | * @param n length of the resulting array 39 | * @param min lower limit of the distribution 40 | * @param max upper limit of the distribution 41 | * 42 | * @return an array of random values between min and max 43 | */ 44 | pub fn runif_n(n int, min f64, max f64) []f64 { 45 | 46 | mut res := [f64(0)].repeat(n) 47 | for i := 0; i < n; i++ { 48 | res[i] = runif(min, max) 49 | } 50 | 51 | return res 52 | } 53 | 54 | /** 55 | * ########################### 56 | * ########## rnorm ########## 57 | * ########################### 58 | */ 59 | 60 | /** 61 | * rnorm - random number generator with a normal distribution 62 | * 63 | * Uses the Box Muller transform algorithm to get normally distributed 64 | * random numbers from the uniform rand.next(). 65 | * 66 | * @param mean mean of the underlying normal distribution 67 | * @param sd standard deviation of the underlying normal distribution 68 | * 69 | * @return a random value sampled from the defined normal distribution 70 | */ 71 | pub fn rnorm(mean f64, sd f64) f64 { 72 | 73 | mut x := 0.0 74 | mut y := 0.0 75 | mut r := 0.0 76 | 77 | for { 78 | x = 2.0 * f64(rand.next(C.RAND_MAX)) / f64(C.RAND_MAX - 1) 79 | y = 2.0 * f64(rand.next(C.RAND_MAX)) / f64(C.RAND_MAX - 1) 80 | r = x * x + y * y 81 | 82 | if (r != 0.0 && r <= 1.0) { 83 | break 84 | } 85 | } 86 | 87 | d := math.sqrt(-2.0 * math.log(r) / r) 88 | n1 := x * d 89 | //n2 := y * d 90 | res := n1 * sd + mean 91 | 92 | return res 93 | } 94 | 95 | /** 96 | * rnorm_n - rnorm but for an array of random values 97 | * 98 | * Uses rnorm. 99 | * 100 | * @param n length of the resulting array 101 | * @param mean mean of the underlying normal distribution 102 | * @param sd standard deviation of the underlying normal distribution 103 | * 104 | * @return an array of random values sampled from the defined normal distribution 105 | */ 106 | pub fn rnorm_n(n int, mean f64, sd f64) []f64 { 107 | 108 | mut res := [f64(0)].repeat(n) 109 | for i := 0; i < n; i++ { 110 | res[i] = rnorm(mean, sd) 111 | } 112 | 113 | return res 114 | } 115 | 116 | /** 117 | * ########################### 118 | * ########## rarb ########### 119 | * ########################### 120 | */ 121 | 122 | 123 | /** 124 | * rarb_int - random number selector with an arbitrary distribution 125 | * 126 | * Returns a random number from arr[] according to the 127 | * distribution array defined by freq[]. The code was translated from 128 | * https://www.geeksforgeeks.org/random-number-generator-in-arbitrary-probability-distribution-fashion 129 | * 130 | * @param arr[] array from which the output numbers are selected 131 | * @param freq[] array of the same length n as arr[] that assigns a probability to that value 132 | * 133 | * @return a random value sampled from arr[] based on the distribution in freq[] 134 | */ 135 | pub fn rarb_int(arr[] int, freq[] int) int { 136 | // get length 137 | l := arr.len 138 | // Create and fill prefix array 139 | mut prefix := [0].repeat(l) 140 | prefix[0] = freq[0] 141 | for i := 1; i < l; i++ { 142 | prefix[i] = prefix[i - 1] + freq[i] 143 | } 144 | // prefix[n-1] is sum of all frequencies. Generate a random number 145 | // with value from 1 to this sum 146 | r := (rand.next(prefix[l - 1])) + 1 147 | // Find index of ceiling of r in prefix array 148 | indexc := find_ceil(prefix, r, 0, l - 1) 149 | return arr[indexc] 150 | } 151 | 152 | // Utility function to find ceiling of r in arr[l..h] 153 | fn find_ceil(arr[] int, r int, l int, h int) int { 154 | mut mid := 0 155 | mut le := l 156 | mut he := h 157 | for { 158 | mid = (le + he) / 2 159 | if r > arr[mid] { le = mid + 1 } else { he = mid } 160 | if !(le < he) { break } 161 | } 162 | mut res := 0 163 | if arr[le] >= r { res = le } else { res = -1 } 164 | return res 165 | } 166 | 167 | /** 168 | * rarb_int_n - rarb_int but for an array of random values 169 | * 170 | * Uses rarb_int. 171 | * 172 | * @param n length of the resulting array 173 | * @param arr[] array from which the output numbers are selected 174 | * @param freq[] array of the same length n as arr[] that assigns a probability to that value 175 | * 176 | * @return an array of random values sampled from from arr[] based on the distribution in freq[] 177 | */ 178 | pub fn rarb_int_n(n int, arr[] int, freq[] int) []int { 179 | 180 | mut res := [0].repeat(n) 181 | for i := 0; i < n; i++ { 182 | res[i] = rarb_int(arr, freq) 183 | } 184 | 185 | return res 186 | } 187 | 188 | /** 189 | * ############################ 190 | * ########## sample ########## 191 | * ############################ 192 | */ 193 | 194 | pub fn sample(arr[] T, size int) []T { 195 | 196 | mut res := [arr[1]].repeat(size) 197 | for i := 0; i < size; i++ { 198 | res[i] = arr[rand.next(arr.len)] 199 | } 200 | 201 | return res 202 | 203 | } 204 | --------------------------------------------------------------------------------