If s is constant, the generator output will be the same each time the program runs. If s is based on the current value of the system clock, the generator output will be different each time.
随机种子生成器
1 2 3 4 5 6 7 8 9 10 11 12 13
package main
import ( "fmt" "math/rand" )
funcmain() { var s int64 = 42 rand.Seed(s) fmt.Println(rand.Int()) }
3440579354231278675
or
1 2 3 4 5 6 7 8 9 10 11 12
package main
import ( "fmt" "math/rand" )
funcmain() { var s int64 = 42 r := rand.New(rand.NewSource(s)) fmt.Println(r.Int()) }
Get the current datetime and provide it as a seed to a random generator. The generator sequence will be different at each run.
使用时钟作为随机生成器的种子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
package main
import ( "fmt" "math/rand" "time" )
funcmain() { rand.Seed(time.Now().UnixNano()) // Well, the playground date is actually fixed in the past, and the // output is cached. // But if you run this on your workstation, the output will vary. fmt.Println(rand.Intn(999)) }
524
or
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
package main
import ( "fmt" "math/rand" "time" )
funcmain() { r := rand.New(rand.NewSource(time.Now().UnixNano())) // Well, the playground date is actually fixed in the past, and the // output is cached. // But if you run this on your workstation, the output will vary. fmt.Println(r.Intn(999)) }
524
1 2 3 4 5 6 7 8 9 10 11
use rand::{Rng, SeedableRng, rngs::StdRng}; use std::time::SystemTime;
Basic implementation of the Echo program: Print all arguments except the program name, separated by space, followed by newline. The idiom demonstrates how to skip the first argument if necessary, concatenate arguments as strings, append newline and print it to stdout.
use itertools::Itertools; println!("{}", std::env::args().skip(1).format(" "));
74. Compute GCD
Compute the greatest common divisor x of big integers a and b. Use an integer type able to handle huge numbers.
计算大整数a和b的最大公约数x。使用能够处理大数的整数类型。
1 2 3 4 5 6 7 8 9 10 11 12 13
package main
import"fmt" import"math/big"
funcmain() { a, b, x := new(big.Int), new(big.Int), new(big.Int) a.SetString("6000000000000", 10) b.SetString("9000000000000", 10) x.GCD(nil, nil, a, b) fmt.Println(x) }
Compute the least common multiple x of big integers a and b. Use an integer type able to handle huge numbers.
1 2 3 4 5 6 7 8 9 10 11 12 13
package main
import"fmt" import"math/big"
funcmain() { a, b, gcd, x := new(big.Int), new(big.Int), new(big.Int), new(big.Int) a.SetString("6000000000000", 10) b.SetString("9000000000000", 10) gcd.GCD(nil, nil, a, b) x.Div(a, gcd).Mul(x, b) fmt.Println(x) }
fnmain() { leti = 5; letf = i asf64; println!("int {:?}, float {:?}", i, f); }
1
int 5, float 5.0
80. Truncate floating point number to integer
Declare integer y and initialize it with the value of floating point number x . Ignore non-integer digits of x . Make sure to truncate towards zero: a negative x must yield the closest greater integer (not lesser).
浮点型转整型
1 2 3 4 5 6 7 8 9 10 11 12
package main
import"fmt"
funcmain() { a := -6.4 b := 6.4 c := 6.6 fmt.Println(int(a)) fmt.Println(int(b)) fmt.Println(int(c)) }
1 2 3
-6 6 6
1 2 3 4 5
fnmain() { letx = 41.59999999f64; lety = x asi32; println!("{}", y); }