GithubHelp home page GithubHelp logo

rndrec's Introduction

rndrec

MIT licensed GoDoc Language Report Card

Package rndrec is used to randomly select records from a pool based on their relative weight. For example, if the relative weight of one record is 50, on average it will be selected five times more often than a record with a relative weight of 10. This is useful for generating plausible data sets for testing purposes, for example names based on frequency or regions based on population.

Example

Given a file named "continent_population.csv" with the following contents,

Africa|1,030,400,000
Antarctica|0
Asia|4,157,300,000
Australia|36,700,000
Europe|738,600,000
North America|461,114,000
South America|390,700,000

the following call will create a weighted record sample source:

var r *SrcType
var err error

r, err = NewRandomRecordSourceFromFile("continent_population.csv", 1, '|', 0)

The integer argument following the filename is the zero-based column that contains the relative weights in numeric form. Note that the commas in these values are disregarded. The rune argument following the weight column specifies the field separator. All input records are assumed to be delimited with newlines. The final argument is the seed value for the instance's random number source. This can be used to generate repeatable sequences. time.Now().Unix() can be used if repeatable sequences are not desired.

Call r.Record() to randomly retrieve weighted records:

for row := 0; row < 8; row++ {
	for col := 0; col < 8; col++ {
		if col > 0 {
			fmt.Printf(" | ")
		}
		rec = r.Record()
		fmt.Printf("%s", rec[0])
	}
	fmt.Println("")
}

This will generate the following ouput:

South America | Asia | Asia | Africa | Asia | Asia | Asia | Asia
North America | Asia | Asia | North America | Europe | Asia | Asia | Asia
Europe | Africa | Europe | Europe | Asia | Asia | Asia | Asia
Asia | Asia | Asia | Asia | Asia | Asia | Africa | Asia
Asia | Asia | Asia | Asia | Asia | Asia | Asia | Africa
Asia | Africa | Asia | Asia | Europe | Africa | North America | North America
Asia | Europe | Africa | Europe | Asia | South America | Africa | Europe
Asia | Europe | Africa | Asia | Asia | Asia | Asia | Africa

Installation

To install the package on your system, run

go get github.com/jung-kurt/rndrec

License

rndrec is released under the MIT License.

rndrec's People

Contributors

jung-kurt avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.