Created
January 14, 2016 15:32
-
-
Save codecakes/475dbdff79ed5e62648a to your computer and use it in GitHub Desktop.
How To Really Effectively Generate random numbers/strings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Any Generator outputs an element x of type (Rational, Integer, Real, Complex, Character Set(s), any other type) from its Set X | |
s.t. x \subset X from a Given Set X or more. | |
So a pool of elements already given, from an academic point of view how is the RANDOMNESS in a function achieved? | |
''' | |
import random | |
def swap(minIndex, j, arr): | |
'''Swap elements from two given positions''' | |
arr[minIndex], arr[j] = arr[j], arr[minIndex] | |
def rangeRand (min, max): | |
''' Generates a random number between min and max excluding max | |
There is a gap between two endpoint integers and that's the delta. | |
The minimum number that generator can generator is the minima/lowest of the two endpoints, | |
that's why you do (delta and generate a random Integer between 0 and 1) + minimum number''' | |
return (random.random() * (max-min)) + min; | |
def shuffle (arr): | |
'''in-place Shuffle from a given Set X; list of elements''' | |
index=0 | |
ln = arr.length | |
while (index<ln): | |
# ~O(N) | |
# to be truly uniformly random, min and max are least and present greatest value | |
index += 1 | |
swap(rangeRand(0, index), index, arr) | |
return arr; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment