4
4
4utka_pyan2017-06-20 08:04:04
go
4utka_pyan, 2017-06-20 08:04:04

Random number in a range in golang?

1. How to generate a random (pseudo-random?) number in the range from 1 to 1000 inclusive?
rand.Intn() - generates in the same environment, the same numbers as I understood
rand.Seed() - no matter how I tried to make it work, I failed
2.
Question on https://tour.golang.org/basics/ 2
fmt.Printf("Now you have %g problems.", math.Sqrt(7))
And the same question at https://tour.golang.org/basics/11
fmt.Printf("Type: %T Value : %v\n", ToBe, ToBe)
How do %g, %T work because they are not defined anywhere, and why is there emptiness instead of %v?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
RidgeA, 2017-06-20
@4utka_pyan

To get a random number, you must first:
1. https://golang.org/pkg/math/rand/#Seed - pass some value as a parameter. Usually the current time is transmitted. If each time you give the same value - the sequence of "random", or rather pseudo-random, numbers will be the same.
2. https://golang.org/pkg/math/rand/#Intn - for range

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question