Probability based event generation

I recently gave an interview in which I was asked about using a function that gives a random number between 0 and 1 to generate events according to a given probability distribution. Following is my solution to it.

Get the random number between 0 and 1.

Loop over the available events while adding their respective probabilities until the sum is equal to or greater than the random number generated and when the sum exceeds the generated random number return the respective event.






MS Thesis Student, CVGL, LUMS

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Invention of Imaginary Numbers

What does the rank of a matrix tell us about the solution?

Bhanu Prakash — A 21 Year Old — World Record Holder — The Fastest Human Calculator

Man Person Male — Free photo on Pixabay

Mibology is the study of marbles.

Birthday Paradox for the Facebook Era

Algebraic, Geometric and Linear Algebraic intuition of “Dot-Product” and how to apply this to…

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Talha Hanif Butt

Talha Hanif Butt

MS Thesis Student, CVGL, LUMS

More from Medium

ML enginee measures consumer confidence on Twitter

What is sparse Matrix?

Prim’s Algorithm

What is Edge Machine Learning?

Edge Machine Learning