Random Number Generation in Go (Challenge)

GoGoBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

This challenge aims to test your ability to generate random numbers using the math/rand package in Golang.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL go(("`Go`")) -.-> go/AdvancedTopicsGroup(["`Advanced Topics`"]) go/AdvancedTopicsGroup -.-> go/random_numbers("`Random Numbers`") subgraph Lab Skills go/random_numbers -.-> lab-15415{{"`Random Number Generation in Go (Challenge)`"}} end

Random Numbers

You are required to write a program that generates random integers and floats within a specified range. The program should also be able to produce varying sequences of numbers by changing the seed.

Requirements

  • The program should use the math/rand package to generate random numbers.
  • The program should generate random integers within a specified range.
  • The program should generate random floats within a specified range.
  • The program should be able to produce varying sequences of numbers by changing the seed.

Example

## Depending on where you run this sample, some of the
## generated numbers may be different. Note that on
## the Go playground seeding with `time.Now()` still
## produces deterministic results due to the way the
## playground is implemented.
$ go run random-numbers.go
81,87
0.6645600532184904
7.123187485356329,8.434115364335547
0,28
5,87
5,87

## See the [`math/rand`](https://pkg.go.dev/math/rand)
## package docs for references on other random quantities
## that Go can provide.

Summary

This challenge requires you to generate random numbers using the math/rand package in Golang. You should be able to generate random integers and floats within a specified range and produce varying sequences of numbers by changing the seed.

Other Go Tutorials you may like