1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

rand: reorganize: phase 2

This commit is contained in:
Hungry Blue Dev
2020-06-09 18:36:07 +05:30
committed by GitHub
parent 67fcce2d46
commit e649cf84e3
28 changed files with 603 additions and 408 deletions

View File

@ -236,7 +236,7 @@ fn intersect(r Ray, spheres &Sphere, nspheres int) (bool, f64, int){
// some casual random function, try to avoid the 0
fn rand_f64() f64 {
x := (C.rand()+1) & 0x3FFF_FFFF
x := (rand.intn(cache_len)+1) & 0x3FFF_FFFF
return f64(x)/f64(0x3FFF_FFFF)
}
@ -318,7 +318,7 @@ fn radiance(r Ray, depthi int, scene_id int) Vec {
//r1 := f64(2.0 * math.pi) * rand_f64()
// tabbed speed-up
r1 := C.rand() & cache_mask
r1 := rand.intn(cache_len) & cache_mask
r2 := rand_f64()
r2s := math.sqrt(r2)
@ -465,9 +465,8 @@ fn main() {
height = os.args[5].int()
}
// init the rand, using the same seed allows to obtain the same result in different runs
// change the seed from 2020 for different results
rand.seed(2020)
// change the seed for a different result
rand.seed([u32(2020), 0])
t1:=time.ticks()

View File

@ -7,11 +7,10 @@ const (
)
fn main() {
rand.seed(int(time.now().unix))
rand.next(gen_max) // skip the first
rand.intn(gen_max) // skip the first
mut arr := []int{}
for _ in 0..gen_len {
arr << rand.next(gen_max)
arr << rand.intn(gen_max)
}
println('length of random array is $arr.len')
println('before quick sort whether array is sorted: ${is_sorted(arr)}')

View File

@ -2,9 +2,7 @@ import rand
import time
fn main() {
rand.seed(int(time.now().unix))
for _ in 0..10 {
println('${rand.next(255)}.${rand.next(255)}.${rand.next(255)}.${rand.next(255)}')
println('${rand.intn(255)}.${rand.intn(255)}.${rand.intn(255)}.${rand.intn(255)}')
}
}

View File

@ -193,7 +193,6 @@ fn main() {
fn (mut g Game) init_game() {
g.parse_tetros()
rand.seed(int(time.now().unix))
g.generate_tetro()
g.field = []
// Generate the field, fill it with 0's, add -1's on each edge
@ -300,7 +299,7 @@ fn (mut g Game) delete_completed_line(y int) {
fn (mut g Game) generate_tetro() {
g.pos_y = 0
g.pos_x = field_width / 2 - tetro_size / 2
g.tetro_idx = rand.next(b_tetros.len)
g.tetro_idx = rand.intn(b_tetros.len)
g.rotation_idx = 0
g.get_tetro()
}

View File

@ -81,7 +81,7 @@ fn get_bet(money int) int {
}
bet = line.int()
if bet <= 0 {
println('error: $line is not heigher than 1.')
println('error: $line is not higher than 1.')
continue
} else if bet > money {
println('error: $line is more money than you have.')
@ -92,8 +92,7 @@ fn get_bet(money int) int {
fn run_wheel(bet_nbr int, _bet int) int {
mut bet := _bet
rand.seed(int(time.now().unix))
winning_nbr := rand.next(50)
winning_nbr := rand.intn(50)
print('Roulette Wheel spinning... and stops on the number $winning_nbr which is a ')
if winning_nbr % 2 == 1 {
println(odd)
@ -115,7 +114,7 @@ fn run_wheel(bet_nbr int, _bet int) int {
fn is_broke(money int) bool {
if money <= 0 {
println('You\'broke, the game is over..')
println('You\'re broke, the game is over..')
return false
} else {
quit := Options{'yes', 'y'}