pjebs / spherand

Random points on a sphere in Golang

Home Page:https://godoc.org/github.com/mmcloughlin/spherand

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

spherand

Random points on a sphere in Golang.

go.dev Reference Build status

The Problem

If you generate latitude and longitude uniformly in the ranges [-90, 90] and [-180, 180] you will get distributions biased towards the poles.

Distribution of broken generator

This package correctly picks points on a sphere, providing a uniform distribution visualized below.

Distribution of correct generator

Getting Started

Install with

$ go get -u github.com/mmcloughlin/spherand

Generate a geographical point with

lat, lng := spherand.Geographical()

You can also generate spherical coordinates with Spherical().

If you need to control the random source, use a generator

g := spherand.NewGenerator(rand.New(rand.NewSource(42)))
lat, lng := g.Geographical()

See go.dev for reference.

Diagrams

Generated with globe.

About

Random points on a sphere in Golang

https://godoc.org/github.com/mmcloughlin/spherand

License:ISC License


Languages

Language:Go 97.8%Language:Makefile 2.2%