Coder Social home page Coder Social logo

xaionaro-go / weightedshuffle Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 12 KB

Shuffle slice with weighted position preference

License: Creative Commons Zero v1.0 Universal

Go 100.00%
weighted weight sort order reorder slice go golang random rand

weightedshuffle's Introduction

GoDoc

Shuffle, but with position preference

This package allows to (semi-)randomly order a slice with weight-based preference to get closer to the beginning of the slice.

For example, slice [1, 2, 3, 4] with weights [0, 1, 9, 10] most likely will result into [4, 3, 2, 1] or [3, 4, 2, 1]:

  • 1 is always in the end because it has zero-weight,
  • 2 most likely goes after 3 and 4 because it has much lower weight
  • while 4 and 3 have almost the same weight so they will have quite random order relatively to each other.

Performance

goos: linux
goarch: amd64
pkg: github.com/xaionaro-go/weightedshuffle
cpu: Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz
BenchmarkShuffle/0/Shuffle-12         	 6839139	       180.2 ns/op	     120 B/op	       3 allocs/op
BenchmarkShuffle/0/ShuffleInplace-12  	 8649206	       139.8 ns/op	      56 B/op	       2 allocs/op
BenchmarkShuffle/1/Shuffle-12         	 4564108	       262.4 ns/op	     144 B/op	       4 allocs/op
BenchmarkShuffle/1/ShuffleInplace-12  	11854474	        87.38 ns/op	      56 B/op	       2 allocs/op
BenchmarkShuffle/10/Shuffle-12        	 1289701	       954.4 ns/op	     424 B/op	      10 allocs/op
BenchmarkShuffle/10/ShuffleInplace-12 	 1888671	       627.1 ns/op	     120 B/op	       5 allocs/op
BenchmarkShuffle/100/Shuffle-12       	  114979	      9761 ns/op	    2216 B/op	      13 allocs/op
BenchmarkShuffle/100/ShuffleInplace-12         	  127245	      9563 ns/op	     120 B/op	       5 allocs/op
BenchmarkShuffle/1000/Shuffle-12               	    7822	    132306 ns/op	   16552 B/op	      16 allocs/op
BenchmarkShuffle/1000/ShuffleInplace-12        	    2047	    613564 ns/op	     120 B/op	       5 allocs/op
BenchmarkShuffle/10000/Shuffle-12              	     688	   1745520 ns/op	  386473 B/op	      25 allocs/op
BenchmarkShuffle/10000/ShuffleInplace-12       	      19	  59876093 ns/op	     120 B/op	       5 allocs/op
BenchmarkShuffle/100000/Shuffle-12             	      60	  23312442 ns/op	 4654532 B/op	      35 allocs/op
BenchmarkShuffle/1000000/Shuffle-12            	       4	 282116172 ns/op	45188534 B/op	      45 allocs/op
PASS
ok  	github.com/xaionaro-go/weightedshuffle	20.833s

weightedshuffle's People

Contributors

xaionaro avatar

Stargazers

 avatar  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.