mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
js: fix rand
build, properly use key values on map, add rand.string
(#12020)
This commit is contained in:
@ -125,3 +125,16 @@ pub fn hex(len int) string {
|
||||
pub fn ascii(len int) string {
|
||||
return string_from_set(ascii_chars, len)
|
||||
}
|
||||
|
||||
fn deinit() {
|
||||
unsafe {
|
||||
default_rng.free() // free the implementation
|
||||
free(default_rng) // free the interface wrapper itself
|
||||
}
|
||||
}
|
||||
|
||||
// init initializes the default RNG.
|
||||
fn init() {
|
||||
default_rng = new_default()
|
||||
C.atexit(deinit)
|
||||
}
|
||||
|
17
vlib/rand/rand.js.v
Normal file
17
vlib/rand/rand.js.v
Normal file
@ -0,0 +1,17 @@
|
||||
module rand
|
||||
|
||||
// init initializes the default RNG.
|
||||
fn init() {
|
||||
default_rng = new_default()
|
||||
}
|
||||
|
||||
pub fn string(len int) string {
|
||||
result := ''
|
||||
#
|
||||
#const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
||||
#const charactersLength = characters.length;
|
||||
#for (let i = 0;i < len.val;i++)
|
||||
#result.str += characters.charAt(Math.random() * charactersLength);
|
||||
|
||||
return result
|
||||
}
|
@ -38,19 +38,6 @@ __global (
|
||||
default_rng &PRNG
|
||||
)
|
||||
|
||||
// init initializes the default RNG.
|
||||
fn init() {
|
||||
default_rng = new_default()
|
||||
C.atexit(deinit)
|
||||
}
|
||||
|
||||
fn deinit() {
|
||||
unsafe {
|
||||
default_rng.free() // free the implementation
|
||||
free(default_rng) // free the interface wrapper itself
|
||||
}
|
||||
}
|
||||
|
||||
// new_default returns a new instance of the default RNG. If the seed is not provided, the current time will be used to seed the instance.
|
||||
[manualfree]
|
||||
pub fn new_default(config config.PRNGConfigStruct) &PRNG {
|
||||
|
7
vlib/rand/rand_test.js.v
Normal file
7
vlib/rand/rand_test.js.v
Normal file
@ -0,0 +1,7 @@
|
||||
import rand
|
||||
|
||||
fn test_string() {
|
||||
res := rand.string(4)
|
||||
assert res.len == 4
|
||||
println(res)
|
||||
}
|
3
vlib/rand/wyrand/wyrand.js.v
Normal file
3
vlib/rand/wyrand/wyrand.js.v
Normal file
@ -0,0 +1,3 @@
|
||||
module wyrand
|
||||
|
||||
pub fn (mut r WyRandRNG) free() {}
|
@ -250,9 +250,3 @@ pub fn (mut rng WyRandRNG) f64_in_range(min f64, max f64) f64 {
|
||||
}
|
||||
return min + rng.f64n(max - min)
|
||||
}
|
||||
|
||||
// free should be called when the generator is no longer needed
|
||||
[unsafe]
|
||||
pub fn (mut rng WyRandRNG) free() {
|
||||
unsafe { free(rng) }
|
||||
}
|
||||
|
7
vlib/rand/wyrand/z_wyrand.c.v
Normal file
7
vlib/rand/wyrand/z_wyrand.c.v
Normal file
@ -0,0 +1,7 @@
|
||||
module wyrand
|
||||
|
||||
// free should be called when the generator is no longer needed
|
||||
[unsafe]
|
||||
pub fn (mut rng WyRandRNG) free() {
|
||||
unsafe { free(rng) }
|
||||
}
|
Reference in New Issue
Block a user