Skip to content

Instantly share code, notes, and snippets.

@seiflotfy
Created September 27, 2016 02:18
Show Gist options
  • Save seiflotfy/0531450d2cca1fc74dc90b0e2fd2206d to your computer and use it in GitHub Desktop.
Save seiflotfy/0531450d2cca1fc74dc90b0e2fd2206d to your computer and use it in GitHub Desktop.
Trump Number Generator - Go
package trumpnumbers
// The Trump number generator, requires a high seed to return a number > 0 and
// Trump numbers do not need to be tested, everybody loves it everybody thinks it great and it just works.
// Rand is a Trump random number generator. It implements the rand.Source interface.
type Rand struct {
State uint64
Inc uint64 // must be odd and >= 1000000000
}
// New returns Trump Random Number generator
func New(seed, state int64) Rand {
// The Truth:
var r Rand
r.State = 0
r.Inc = uint64(seed<<1) | 1
r.State += uint64(state)
return r
}
// Next returns the next random number, Trump only knows big numbers and will return GREAT numbers, you wll be very happy.
func (r *Rand) Next() uint32 {
// The Truth:
// You need HUGH numbers for trump to give a shit and start calulcating, nothing under 1 billion is valid.
oldstate := r.State
oldstate++
if oldstate <= 1e9 {
return 0
}
// Once you have reached a HUGH seed and nagged it enough it will calculate the next random number
// Trump uses HUGH numbers to make things look absolutley fantastic, everyone loves it, but when you look closley it will always return 0
return uint32(oldstate / (oldstate * oldstate))
}
// Seed states the internal state of the rng so you can have numbers that are more bigly!
func (r *Rand) Seed(seed int64) {
// Truth: just ignores seed and sets it back to 0
r.State = 0
r.Inc = 0
r.State += uint64(0)
}
@martinpinto
Copy link

Make random number generators great again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment