SlideShare a Scribd company logo
IX. ON RANDOMNESS
Engr. RANEL O. PADON
PYTHON PROGRAMMING TOPICS
I

•Introduction to Python Programming

II

•Python Basics

III

•Controlling the Program Flow

IV

•Program Components: Functions, Classes, Packages, and Modules

V

•Sequences (List and Tuples), and Dictionaries

VI

•Object-Based Programming: Classes and Objects

VII

•Customizing Classes and Operator Overloading

VIII

•Object-Oriented Programming: Inheritance and Polymorphism

IX

•Randomization Algorithms

X

•Exception Handling and Assertions

XI

•String Manipulation and Regular Expressions

XII

•File Handling and Processing

XIII

•GUI Programming Using Tkinter
ON RANDOMNESS

RANEL O. PADON
ON RANDOMNESS

RANEL O. PADON
ON RANDOMNESS
Randomization Algorithms
* Random numbers are used for testing the performance of programs,
creating scientific simulations, and so on.

* A pseudorandom number generator (PRNG) is an algorithm for
generating a sequence of numbers that approximates the properties
of random numbers.
* The sequence is not truly random in that it is completely
determined by a relatively small set of initial values (random seed)
ON RANDOMNESS Randomization Algorithms
Early Algorithm
Middle-Square Method
* take any number, square it, remove the middle digits of the
resulting number as the "random number", then use that number as
the seed for the next iteration.
* squaring the number "1111" yields "1234321", which can be written
as "01234321", an 8-digit number being the square of a 4-digit
number. This gives "2343" as the "random" number. Repeating this
procedure gives "4896" as the next result, and so on.
ON RANDOMNESS Randomization Algorithms
1.) Blum Blum Shub (cryptographically sound, but slow)
2.) Mersenne Twister
- fast with good statistical properties,
- used when cyptography is not an issue
- used in Python
3.) Linear Congruential Generator
- commonly-used in compilers

and many, many others
ON RANDOMNESS Randomization Algorithms
2.) Mersenne Twister Algorithm
is based on a matrix linear recurrence over a finite binary field
provides for fast generation of very high-quality pseudorandom
numbers, having been designed specifically to rectify many of the
flaws found in older algorithms.
used in PHP, Ruby and Python
name derives from the fact that period length is chosen to be a
Mersenne prime
ON RANDOMNESS Mersenne Twister Algorithm
2.) Mersenne Twister Algorithm
It has a very long period of 219937 − 1. While a long period is
not a guarantee of quality in a random number generator, short
periods (such as the 232 common in many software packages)
can be problematic.
It is k-distributed to 32-bit accuracy for every 1 ≤ k ≤ 623

It passes numerous tests for statistical randomness, including the
Diehard tests. It passes most, but not all, of the even more
stringent TestU01 Crush randomness tests.
ON RANDOMNESS Mersenne Twister Algorithm
2.) Mersenne Twister Algorithm
(as used in the random module of Python)
# Create a length 624 list to store the state of the generator
MT = [0 for i in xrange(624)]
index = 0

# To get last 32 bits
bitmask_1 = (2 ** 32) - 1
# To get 32. bit
bitmask_2 = 2 ** 31
# To get last 31 bits
bitmask_3 = (2 ** 31) - 1
ON RANDOMNESS Mersenne Twister Algorithm
2.) Mersenne Twister Algorithm
def initialize_generator(seed):
"Initialize the generator from a seed"
global MT
global bitmask_1
MT[0] = seed
for i in xrange(1,624):
MT[i] = ((1812433253 * MT[i-1]) ^ ((MT[i-1] >> 30) + i)) &
bitmask_1
ON RANDOMNESS Mersenne Twister Algorithm
def extract_number():
""“ Extract a tempered pseudorandom number based on the indexth value, calling generate_numbers() every 624 numbers ""“
global index
global MT
if index == 0:
generate_numbers()
y = MT[index]
y ^= y >> 11
y ^= (y << 7) & 2636928640
y ^= (y << 15) & 4022730752
y ^= y >> 18
index = (index + 1) % 624
return y
ON RANDOMNESS Mersenne Twister Algorithm
def generate_numbers():
"Generate an array of 624 untempered numbers"
global MT
for i in xrange(624):
y = (MT[i] & bitmask_2) + (MT[(i + 1 ) % 624] & bitmask_3)
MT[i] = MT[(i + 397) % 624] ^ (y >> 1)
if y % 2 != 0:
MT[i] ^= 2567483615
if __name__ == "__main__":
from datetime import datetime
now = datetime.now()
initialize_generator(now.microsecond)
for i in xrange(100):
"Print 100 random numbers as an example"
print extract_number()
ON RANDOMNESS Linear Congruential Generator
3.) Linear Congruential Generator
represents one of the oldest and best-known pseudorandom number
generator algorithms.

the theory behind them is easy to understand, and they are easily
implemented and fast.
ON RANDOMNESS Linear Congruential Generator
3.) Linear Congruential Generator
ON RANDOMNESS Randomization Algorithms
3.) Linear Congruential Generator
The basic idea is to multiply the last number with a factor a, add a
constant c and then modulate it by m.
Xn+1 = (aXn + c) mod m.
where X0 is the seed.
ON RANDOMNESS Random Seed

Random Seed
* a number (or vector) used to initialize a pseudorandom number
generator
* crucial in the field of computer security.
* having the seed will allow one to obtain the secret encryption key
in a pseudorandomly generated encryption values
ON RANDOMNESS Random Seed

Random Seed

* two or more systems using matching pseudorandom number
algorithms and matching seeds can generate matching sequences of
non-repeating numbers which can be used to synchronize remote
systems, such as GPS satellites and receivers.
ON RANDOMNESS Linear Congruential Generator
a=3
c=9
m = 16
xi = 0
def seed(x):
global xi
xi = x

Random
Number
Generator

def rng():
global xi
xi = (a*xi + c) % m
return xi
for i in range(10):
print rng()
ON RANDOMNESS Linear Congruential Generator
LCG (Standard Parameters)

Good One
ON RANDOMNESS Linear Congruential Generator
Using
java.util.Random
parameters

a = 25214903917
c = 11
m = 2**48
xi = 1000
def seed(x):
global xi
xi = x
def rng():
global xi
xi = (a*xi + c) % m
return xi
def rng_bounded(low, high):
return low + rng()%(high - low+1)
for i in range(10):
rng_bounded(1, 10)
ON RANDOMNESS Minimal Standard
MINSTD by Park & Miller (1988)
known as the Minimal Standard Random number generator
a very good set of parameters for LCG:

m = 231 − 1 = 2,147,483,647 (a Mersenne prime M31)
a = 75 = 16,807 (a primitive root modulo M31)
c=0
often the generator that used for the built in random number function in
compilers and other software packages.
used in Apple CarbonLib, a procedural API for developing Mac OS X
applications
ON RANDOMNESS Linear Congruential Generator
Using
MINSTD
parameters

a = 7**5
c=0
m = 2**31 - 1
xi = 1000
def seed(x):
global xi
xi = x
def rng():
global xi
xi = (a*xi + c) % m
return xi
def rng_bounded(low, high):
return low + rng() % (high - low+1)
for i in range(10):
rng_bounded(1, 2)
ON RANDOMNESS Linear Congruential Generator
Tossing 1 Coin
using LCG MINSTD
(1 million times)

from __ future__ import division
if __name__ == '__main__':
main()
a = 7**5
c=0
m = 2**31 - 1
xi = 1000
def seed(x):
global xi
xi = x

def rng():
global xi
xi = (a*xi + c) % m
return xi
ON RANDOMNESS Linear Congruential Generator
Tossing 1 Coin
using LCG MINSTD
(1 million times)

def rng_bounded(low, high):
return low + rng() % (high - low+1)
(heads, tails, count) = (0, 0, 0)
for i in range (1, 1000001):
count += 1
coin rng_bounded(1,2)

if coin == 1:
heads += 1
else:
tails += 1
ON RANDOMNESS Linear Congruential Generator
Tossing 1 Coin
using LCG MINSTD
(1 million times)

print "heads is ", heads/count
print "tails is ", tails/count
ON RANDOMNESS Linear Congruential Generator
Tossing 1 Coin
using Python randrange()
(1 million times)

from __ future__ import division
import random
if __name__ == '__main__':
main()
(heads, tails, count) = (0, 0, 0)
random.seed(1000)
for i in range (1,1000001):
count +=1
coin = random.randrange(1,3)
ON RANDOMNESS Linear Congruential Generator
Tossing 1 Coin
using Python randrange()
(1 million times)

if coin == 1:
heads += 1
else:
tails += 1

print "heads is ", heads/count
print "tails is ", tails/count
ON RANDOMNESS Linear Congruential Generator
Tossing 2 Coins
using LCG MINSTD
(1 million times)

from __ future__ import division
if __name__ == '__main__':
main()
a = 7**5
c=0
m = 2**31 - 1
xi = 1000
def seed(x):
global xi
xi = x

def rng():
global xi
xi = (a*xi + c) % m
return xi
ON RANDOMNESS Linear Congruential Generator
Tossing 2 Coins
using LCG MINSTD
(1 million times)

def rng_bounded(low, high):
return low + rng() % (high - low+1)

(heads, tails, combo, count) = (0, 0, 0, 0)
for i in range (1,1000001):
count += 1
coin1 = rng_bounded(1,2)
coin2 = rng_bounded(1,2)
sum = coin1 + coin2
if sum == 2:
heads += 1
elif sum == 4:
tails += 1
else:
combo += 1
ON RANDOMNESS Linear Congruential Generator
Tossing 2 Coins
using LCG MINSTD
(1 million times)

print "head is ", heads/count
print "tail is ", tails/count
print "combo is ", combo/count
ON RANDOMNESS Linear Congruential Generator
Tossing 2 Coins
using Python randrange()
(1 million times)

from __ future__ import division
import random
if __name__ == '__main__':
main()
(heads, tails, combo, count) = (0, 0, 0, 0)
random.seed(1000)
for i in range (1,1000001):
count +=1
coin1 = random.randrange(1,3)
coin2 = random.randrange(1,3)
sum = coin1 + coin2
ON RANDOMNESS Linear Congruential Generator
Tossing 2 Coins
using Python randrange()
(1 million times)

if sum == 2:
heads += 1

elif sum == 4:
tails += 1
else:
combo += 1
print "head is ", heads/count
print "tail is ", tails/count
print "combo is ", combo/count
ON RANDOMNESS Results Summary
Almost Similar Results
Tossing 1 Coin using LCG MINSTD vs Python randrange() (1 million times)

Tossing 2 Coins using LCG MINSTD vs Python randrange() (1 million times)
ON RANDOMNESS Linear Congruential Generator
Tossing 1 Coin using LCG MINSTD (1000 times)
More Elegant Way (Using List)
ON RANDOMNESS Linear Congruential Generator
Tossing 2 Coins
using LCG MINSTD
(1 million times)
More Elegant Way
(Using List)

from __future__ import division

if __name__ == '__main__':
main()
xi = 1000
def seed(x):
global xi
xi = x
def rng(a=7**5, c=0, m = 2**31 - 1):
global xi
xi = (a*xi + c) % m
return xi
def rng_bounded(low, high):
return low + rng() % (high - low+1)
ON RANDOMNESS Linear Congruential Generator
Tossing 2 Coins
using LCG MINSTD
(1 million times)
More Elegant Way

tosses = []
for i in range (1, 1000001):
tosses += [rngNiRanie(1, 2) + rngNiRanie(1, 2)]
print "heads count is ", tosses.count(2) / len(tosses)
print "tails count is ", tosses.count(4) / len(tosses)
print "combo count is ", tosses.count(3) / len(tosses)
ON RANDOMNESS

RANEL O. PADON
ON RANDOMNESS
a = 25214903917
c = 11
m = 2**48
xi = 1000
def seed(x):
global xi
xi = x
def rng():
global xi
xi = (a*xi + c) % m
return xi
def ranie(lowerBound, upperBound):
#ano laman nito? #
for i in range(10):
ranie(1,10)
REFERENCES

 Deitel, Deitel, Liperi, and Wiedermann - Python: How to Program (2001).

 Disclaimer: Most of the images/information used here have no proper source citation, and I do
not claim ownership of these either. I don’t want to reinvent the wheel, and I just want to reuse
and reintegrate materials that I think are useful or cool, then present them in another light,
form, or perspective. Moreover, the images/information here are mainly used for
illustration/educational purposes only, in the spirit of openness of data, spreading light, and
empowering people with knowledge. 

More Related Content

What's hot (19)

Programming in Scala - Lecture Four
Programming in Scala - Lecture FourProgramming in Scala - Lecture Four
Programming in Scala - Lecture Four
Angelo Corsaro
 
Map(), flatmap() and reduce() are your new best friends: simpler collections,...
Map(), flatmap() and reduce() are your new best friends: simpler collections,...Map(), flatmap() and reduce() are your new best friends: simpler collections,...
Map(), flatmap() and reduce() are your new best friends: simpler collections,...
Chris Richardson
 
Functional Programming in Scala: Notes
Functional Programming in Scala: NotesFunctional Programming in Scala: Notes
Functional Programming in Scala: Notes
Roberto Casadei
 
Programming in Scala - Lecture Two
Programming in Scala - Lecture TwoProgramming in Scala - Lecture Two
Programming in Scala - Lecture Two
Angelo Corsaro
 
Introduction to functional programming (In Arabic)
Introduction to functional programming (In Arabic)Introduction to functional programming (In Arabic)
Introduction to functional programming (In Arabic)
Omar Abdelhafith
 
Introduction to objective c
Introduction to objective cIntroduction to objective c
Introduction to objective c
Mayank Jalotra
 
Introduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScriptIntroduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScript
tmont
 
Functional programming
Functional programmingFunctional programming
Functional programming
Christian Hujer
 
Programming in Scala - Lecture Three
Programming in Scala - Lecture ThreeProgramming in Scala - Lecture Three
Programming in Scala - Lecture Three
Angelo Corsaro
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
Tech_MX
 
Learning Functional Programming Without Growing a Neckbeard
Learning Functional Programming Without Growing a NeckbeardLearning Functional Programming Without Growing a Neckbeard
Learning Functional Programming Without Growing a Neckbeard
Kelsey Gilmore-Innis
 
Generic programming and concepts that should be in C++
Generic programming and concepts that should be in C++Generic programming and concepts that should be in C++
Generic programming and concepts that should be in C++
Anton Kolotaev
 
Programming in Scala: Notes
Programming in Scala: NotesProgramming in Scala: Notes
Programming in Scala: Notes
Roberto Casadei
 
Generic Programming
Generic ProgrammingGeneric Programming
Generic Programming
Muhammad Alhalaby
 
Kotlin
KotlinKotlin
Kotlin
YeldosTanikin
 
C++ Templates 2
C++ Templates 2C++ Templates 2
C++ Templates 2
Ganesh Samarthyam
 
Core java by a introduction sandesh sharma
Core java by a introduction sandesh sharmaCore java by a introduction sandesh sharma
Core java by a introduction sandesh sharma
Sandesh Sharma
 
If You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are WrongIf You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are Wrong
Mario Fusco
 
Functional Programming Fundamentals
Functional Programming FundamentalsFunctional Programming Fundamentals
Functional Programming Fundamentals
Shahriar Hyder
 
Programming in Scala - Lecture Four
Programming in Scala - Lecture FourProgramming in Scala - Lecture Four
Programming in Scala - Lecture Four
Angelo Corsaro
 
Map(), flatmap() and reduce() are your new best friends: simpler collections,...
Map(), flatmap() and reduce() are your new best friends: simpler collections,...Map(), flatmap() and reduce() are your new best friends: simpler collections,...
Map(), flatmap() and reduce() are your new best friends: simpler collections,...
Chris Richardson
 
Functional Programming in Scala: Notes
Functional Programming in Scala: NotesFunctional Programming in Scala: Notes
Functional Programming in Scala: Notes
Roberto Casadei
 
Programming in Scala - Lecture Two
Programming in Scala - Lecture TwoProgramming in Scala - Lecture Two
Programming in Scala - Lecture Two
Angelo Corsaro
 
Introduction to functional programming (In Arabic)
Introduction to functional programming (In Arabic)Introduction to functional programming (In Arabic)
Introduction to functional programming (In Arabic)
Omar Abdelhafith
 
Introduction to objective c
Introduction to objective cIntroduction to objective c
Introduction to objective c
Mayank Jalotra
 
Introduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScriptIntroduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScript
tmont
 
Programming in Scala - Lecture Three
Programming in Scala - Lecture ThreeProgramming in Scala - Lecture Three
Programming in Scala - Lecture Three
Angelo Corsaro
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
Tech_MX
 
Learning Functional Programming Without Growing a Neckbeard
Learning Functional Programming Without Growing a NeckbeardLearning Functional Programming Without Growing a Neckbeard
Learning Functional Programming Without Growing a Neckbeard
Kelsey Gilmore-Innis
 
Generic programming and concepts that should be in C++
Generic programming and concepts that should be in C++Generic programming and concepts that should be in C++
Generic programming and concepts that should be in C++
Anton Kolotaev
 
Programming in Scala: Notes
Programming in Scala: NotesProgramming in Scala: Notes
Programming in Scala: Notes
Roberto Casadei
 
Core java by a introduction sandesh sharma
Core java by a introduction sandesh sharmaCore java by a introduction sandesh sharma
Core java by a introduction sandesh sharma
Sandesh Sharma
 
If You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are WrongIf You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are Wrong
Mario Fusco
 
Functional Programming Fundamentals
Functional Programming FundamentalsFunctional Programming Fundamentals
Functional Programming Fundamentals
Shahriar Hyder
 

Viewers also liked (10)

Python Programming - V. Sequences (List and Tuples) and Dictionaries
Python Programming - V. Sequences (List and Tuples) and DictionariesPython Programming - V. Sequences (List and Tuples) and Dictionaries
Python Programming - V. Sequences (List and Tuples) and Dictionaries
Ranel Padon
 
Switchable Map APIs with Drupal
Switchable Map APIs with DrupalSwitchable Map APIs with Drupal
Switchable Map APIs with Drupal
Ranel Padon
 
Python Programming - III. Controlling the Flow
Python Programming - III. Controlling the FlowPython Programming - III. Controlling the Flow
Python Programming - III. Controlling the Flow
Ranel Padon
 
Python Programming - XII. File Processing
Python Programming - XII. File ProcessingPython Programming - XII. File Processing
Python Programming - XII. File Processing
Ranel Padon
 
Python Programming - XI. String Manipulation and Regular Expressions
Python Programming - XI. String Manipulation and Regular ExpressionsPython Programming - XI. String Manipulation and Regular Expressions
Python Programming - XI. String Manipulation and Regular Expressions
Ranel Padon
 
Python Programming - VIII. Inheritance and Polymorphism
Python Programming - VIII. Inheritance and PolymorphismPython Programming - VIII. Inheritance and Polymorphism
Python Programming - VIII. Inheritance and Polymorphism
Ranel Padon
 
Python Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and AssertionsPython Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and Assertions
Ranel Padon
 
Python Programming - VI. Classes and Objects
Python Programming - VI. Classes and ObjectsPython Programming - VI. Classes and Objects
Python Programming - VI. Classes and Objects
Ranel Padon
 
Python Programming - XIII. GUI Programming
Python Programming - XIII. GUI ProgrammingPython Programming - XIII. GUI Programming
Python Programming - XIII. GUI Programming
Ranel Padon
 
Python Programming - I. Introduction
Python Programming - I. IntroductionPython Programming - I. Introduction
Python Programming - I. Introduction
Ranel Padon
 
Python Programming - V. Sequences (List and Tuples) and Dictionaries
Python Programming - V. Sequences (List and Tuples) and DictionariesPython Programming - V. Sequences (List and Tuples) and Dictionaries
Python Programming - V. Sequences (List and Tuples) and Dictionaries
Ranel Padon
 
Switchable Map APIs with Drupal
Switchable Map APIs with DrupalSwitchable Map APIs with Drupal
Switchable Map APIs with Drupal
Ranel Padon
 
Python Programming - III. Controlling the Flow
Python Programming - III. Controlling the FlowPython Programming - III. Controlling the Flow
Python Programming - III. Controlling the Flow
Ranel Padon
 
Python Programming - XII. File Processing
Python Programming - XII. File ProcessingPython Programming - XII. File Processing
Python Programming - XII. File Processing
Ranel Padon
 
Python Programming - XI. String Manipulation and Regular Expressions
Python Programming - XI. String Manipulation and Regular ExpressionsPython Programming - XI. String Manipulation and Regular Expressions
Python Programming - XI. String Manipulation and Regular Expressions
Ranel Padon
 
Python Programming - VIII. Inheritance and Polymorphism
Python Programming - VIII. Inheritance and PolymorphismPython Programming - VIII. Inheritance and Polymorphism
Python Programming - VIII. Inheritance and Polymorphism
Ranel Padon
 
Python Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and AssertionsPython Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and Assertions
Ranel Padon
 
Python Programming - VI. Classes and Objects
Python Programming - VI. Classes and ObjectsPython Programming - VI. Classes and Objects
Python Programming - VI. Classes and Objects
Ranel Padon
 
Python Programming - XIII. GUI Programming
Python Programming - XIII. GUI ProgrammingPython Programming - XIII. GUI Programming
Python Programming - XIII. GUI Programming
Ranel Padon
 
Python Programming - I. Introduction
Python Programming - I. IntroductionPython Programming - I. Introduction
Python Programming - I. Introduction
Ranel Padon
 
Ad

Similar to Python Programming - IX. On Randomness (20)

Random_Number_Generation_Algorithms.pptx
Random_Number_Generation_Algorithms.pptxRandom_Number_Generation_Algorithms.pptx
Random_Number_Generation_Algorithms.pptx
gyanarashmi99
 
lections tha detail aboot andom nummerss
lections tha detail aboot andom nummersslections tha detail aboot andom nummerss
lections tha detail aboot andom nummerss
SandeshStha2
 
J45015460
J45015460J45015460
J45015460
IJERA Editor
 
Amanda Sopkin - Computational Randomness: Creating Chaos in an Ordered Machin...
Amanda Sopkin - Computational Randomness: Creating Chaos in an Ordered Machin...Amanda Sopkin - Computational Randomness: Creating Chaos in an Ordered Machin...
Amanda Sopkin - Computational Randomness: Creating Chaos in an Ordered Machin...
Codemotion
 
AN ALTERNATIVE APPROACH FOR SELECTION OF PSEUDO RANDOM NUMBERS FOR ONLINE EXA...
AN ALTERNATIVE APPROACH FOR SELECTION OF PSEUDO RANDOM NUMBERS FOR ONLINE EXA...AN ALTERNATIVE APPROACH FOR SELECTION OF PSEUDO RANDOM NUMBERS FOR ONLINE EXA...
AN ALTERNATIVE APPROACH FOR SELECTION OF PSEUDO RANDOM NUMBERS FOR ONLINE EXA...
cscpconf
 
Pseudo Random Number
Pseudo Random NumberPseudo Random Number
Pseudo Random Number
Hemant Chetwani
 
Lecture06-Random-Number-Genedawrators.ppt
Lecture06-Random-Number-Genedawrators.pptLecture06-Random-Number-Genedawrators.ppt
Lecture06-Random-Number-Genedawrators.ppt
snhskale
 
4. random number and it's generating techniques
4. random number and it's generating techniques 4. random number and it's generating techniques
4. random number and it's generating techniques
MdFazleRabbi18
 
2. Modelling and Simulation in computer 2.pptx
2. Modelling and Simulation in computer 2.pptx2. Modelling and Simulation in computer 2.pptx
2. Modelling and Simulation in computer 2.pptx
waleedhayyakallah
 
Unit-3 of mathematical foundation of ai ml
Unit-3 of mathematical foundation of ai mlUnit-3 of mathematical foundation of ai ml
Unit-3 of mathematical foundation of ai ml
nikutiwari70
 
Pseudo-Random Number Generators: A New Approach
Pseudo-Random Number Generators: A New ApproachPseudo-Random Number Generators: A New Approach
Pseudo-Random Number Generators: A New Approach
Nithin Prince John
 
Hd3512461252
Hd3512461252Hd3512461252
Hd3512461252
IJERA Editor
 
Random number generation (in C++) – past, present and potential future
Random number generation (in C++) – past, present and potential future Random number generation (in C++) – past, present and potential future
Random number generation (in C++) – past, present and potential future
Pattabi Raman
 
FPGA-based Design System for a Two-Segment Fibonacci LFSR Random Number Gener...
FPGA-based Design System for a Two-Segment Fibonacci LFSR Random Number Gener...FPGA-based Design System for a Two-Segment Fibonacci LFSR Random Number Gener...
FPGA-based Design System for a Two-Segment Fibonacci LFSR Random Number Gener...
IJECEIAES
 
40120140502003
4012014050200340120140502003
40120140502003
IAEME Publication
 
Two Pseudo-random Number Generators, an Overview
Two Pseudo-random Number Generators, an Overview Two Pseudo-random Number Generators, an Overview
Two Pseudo-random Number Generators, an Overview
Kato Mivule
 
Ppt
PptPpt
Ppt
Amit Agarwal
 
Random thoughts on IoT
Random thoughts on IoTRandom thoughts on IoT
Random thoughts on IoT
Mark Carney
 
International Journal on Cryptography and Information Security (IJCIS)
International Journal on Cryptography and Information Security (IJCIS)International Journal on Cryptography and Information Security (IJCIS)
International Journal on Cryptography and Information Security (IJCIS)
ijcisjournal
 
Cost Optimized Design Technique for Pseudo-Random Numbers in Cellular Automata
Cost Optimized Design Technique for Pseudo-Random Numbers in Cellular AutomataCost Optimized Design Technique for Pseudo-Random Numbers in Cellular Automata
Cost Optimized Design Technique for Pseudo-Random Numbers in Cellular Automata
ijait
 
Random_Number_Generation_Algorithms.pptx
Random_Number_Generation_Algorithms.pptxRandom_Number_Generation_Algorithms.pptx
Random_Number_Generation_Algorithms.pptx
gyanarashmi99
 
lections tha detail aboot andom nummerss
lections tha detail aboot andom nummersslections tha detail aboot andom nummerss
lections tha detail aboot andom nummerss
SandeshStha2
 
Amanda Sopkin - Computational Randomness: Creating Chaos in an Ordered Machin...
Amanda Sopkin - Computational Randomness: Creating Chaos in an Ordered Machin...Amanda Sopkin - Computational Randomness: Creating Chaos in an Ordered Machin...
Amanda Sopkin - Computational Randomness: Creating Chaos in an Ordered Machin...
Codemotion
 
AN ALTERNATIVE APPROACH FOR SELECTION OF PSEUDO RANDOM NUMBERS FOR ONLINE EXA...
AN ALTERNATIVE APPROACH FOR SELECTION OF PSEUDO RANDOM NUMBERS FOR ONLINE EXA...AN ALTERNATIVE APPROACH FOR SELECTION OF PSEUDO RANDOM NUMBERS FOR ONLINE EXA...
AN ALTERNATIVE APPROACH FOR SELECTION OF PSEUDO RANDOM NUMBERS FOR ONLINE EXA...
cscpconf
 
Lecture06-Random-Number-Genedawrators.ppt
Lecture06-Random-Number-Genedawrators.pptLecture06-Random-Number-Genedawrators.ppt
Lecture06-Random-Number-Genedawrators.ppt
snhskale
 
4. random number and it's generating techniques
4. random number and it's generating techniques 4. random number and it's generating techniques
4. random number and it's generating techniques
MdFazleRabbi18
 
2. Modelling and Simulation in computer 2.pptx
2. Modelling and Simulation in computer 2.pptx2. Modelling and Simulation in computer 2.pptx
2. Modelling and Simulation in computer 2.pptx
waleedhayyakallah
 
Unit-3 of mathematical foundation of ai ml
Unit-3 of mathematical foundation of ai mlUnit-3 of mathematical foundation of ai ml
Unit-3 of mathematical foundation of ai ml
nikutiwari70
 
Pseudo-Random Number Generators: A New Approach
Pseudo-Random Number Generators: A New ApproachPseudo-Random Number Generators: A New Approach
Pseudo-Random Number Generators: A New Approach
Nithin Prince John
 
Random number generation (in C++) – past, present and potential future
Random number generation (in C++) – past, present and potential future Random number generation (in C++) – past, present and potential future
Random number generation (in C++) – past, present and potential future
Pattabi Raman
 
FPGA-based Design System for a Two-Segment Fibonacci LFSR Random Number Gener...
FPGA-based Design System for a Two-Segment Fibonacci LFSR Random Number Gener...FPGA-based Design System for a Two-Segment Fibonacci LFSR Random Number Gener...
FPGA-based Design System for a Two-Segment Fibonacci LFSR Random Number Gener...
IJECEIAES
 
Two Pseudo-random Number Generators, an Overview
Two Pseudo-random Number Generators, an Overview Two Pseudo-random Number Generators, an Overview
Two Pseudo-random Number Generators, an Overview
Kato Mivule
 
Random thoughts on IoT
Random thoughts on IoTRandom thoughts on IoT
Random thoughts on IoT
Mark Carney
 
International Journal on Cryptography and Information Security (IJCIS)
International Journal on Cryptography and Information Security (IJCIS)International Journal on Cryptography and Information Security (IJCIS)
International Journal on Cryptography and Information Security (IJCIS)
ijcisjournal
 
Cost Optimized Design Technique for Pseudo-Random Numbers in Cellular Automata
Cost Optimized Design Technique for Pseudo-Random Numbers in Cellular AutomataCost Optimized Design Technique for Pseudo-Random Numbers in Cellular Automata
Cost Optimized Design Technique for Pseudo-Random Numbers in Cellular Automata
ijait
 
Ad

More from Ranel Padon (9)

The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)
The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)
The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)
Ranel Padon
 
CKEditor Widgets with Drupal
CKEditor Widgets with DrupalCKEditor Widgets with Drupal
CKEditor Widgets with Drupal
Ranel Padon
 
Views Unlimited: Unleashing the Power of Drupal's Views Module
Views Unlimited: Unleashing the Power of Drupal's Views ModuleViews Unlimited: Unleashing the Power of Drupal's Views Module
Views Unlimited: Unleashing the Power of Drupal's Views Module
Ranel Padon
 
Batch Scripting with Drupal (Featuring the EntityFieldQuery API)
Batch Scripting with Drupal (Featuring the EntityFieldQuery API)Batch Scripting with Drupal (Featuring the EntityFieldQuery API)
Batch Scripting with Drupal (Featuring the EntityFieldQuery API)
Ranel Padon
 
PyCon PH 2014 - GeoComputation
PyCon PH 2014 - GeoComputationPyCon PH 2014 - GeoComputation
PyCon PH 2014 - GeoComputation
Ranel Padon
 
Power and Elegance - Leaflet + jQuery
Power and Elegance - Leaflet + jQueryPower and Elegance - Leaflet + jQuery
Power and Elegance - Leaflet + jQuery
Ranel Padon
 
Python Programming - IV. Program Components (Functions, Classes, Modules, Pac...
Python Programming - IV. Program Components (Functions, Classes, Modules, Pac...Python Programming - IV. Program Components (Functions, Classes, Modules, Pac...
Python Programming - IV. Program Components (Functions, Classes, Modules, Pac...
Ranel Padon
 
Of Nodes and Maps (Web Mapping with Drupal - Part II)
Of Nodes and Maps (Web Mapping with Drupal - Part II)Of Nodes and Maps (Web Mapping with Drupal - Part II)
Of Nodes and Maps (Web Mapping with Drupal - Part II)
Ranel Padon
 
Web Mapping with Drupal
Web Mapping with DrupalWeb Mapping with Drupal
Web Mapping with Drupal
Ranel Padon
 
The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)
The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)
The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)
Ranel Padon
 
CKEditor Widgets with Drupal
CKEditor Widgets with DrupalCKEditor Widgets with Drupal
CKEditor Widgets with Drupal
Ranel Padon
 
Views Unlimited: Unleashing the Power of Drupal's Views Module
Views Unlimited: Unleashing the Power of Drupal's Views ModuleViews Unlimited: Unleashing the Power of Drupal's Views Module
Views Unlimited: Unleashing the Power of Drupal's Views Module
Ranel Padon
 
Batch Scripting with Drupal (Featuring the EntityFieldQuery API)
Batch Scripting with Drupal (Featuring the EntityFieldQuery API)Batch Scripting with Drupal (Featuring the EntityFieldQuery API)
Batch Scripting with Drupal (Featuring the EntityFieldQuery API)
Ranel Padon
 
PyCon PH 2014 - GeoComputation
PyCon PH 2014 - GeoComputationPyCon PH 2014 - GeoComputation
PyCon PH 2014 - GeoComputation
Ranel Padon
 
Power and Elegance - Leaflet + jQuery
Power and Elegance - Leaflet + jQueryPower and Elegance - Leaflet + jQuery
Power and Elegance - Leaflet + jQuery
Ranel Padon
 
Python Programming - IV. Program Components (Functions, Classes, Modules, Pac...
Python Programming - IV. Program Components (Functions, Classes, Modules, Pac...Python Programming - IV. Program Components (Functions, Classes, Modules, Pac...
Python Programming - IV. Program Components (Functions, Classes, Modules, Pac...
Ranel Padon
 
Of Nodes and Maps (Web Mapping with Drupal - Part II)
Of Nodes and Maps (Web Mapping with Drupal - Part II)Of Nodes and Maps (Web Mapping with Drupal - Part II)
Of Nodes and Maps (Web Mapping with Drupal - Part II)
Ranel Padon
 
Web Mapping with Drupal
Web Mapping with DrupalWeb Mapping with Drupal
Web Mapping with Drupal
Ranel Padon
 

Recently uploaded (20)

Oracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI FoundationsOracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI Foundations
VICTOR MAESTRE RAMIREZ
 
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Safe Software
 
Jira Administration Training – Day 1 : Introduction
Jira Administration Training – Day 1 : IntroductionJira Administration Training – Day 1 : Introduction
Jira Administration Training – Day 1 : Introduction
Ravi Teja
 
ISOIEC 42005 Revolutionalises AI Impact Assessment.pptx
ISOIEC 42005 Revolutionalises AI Impact Assessment.pptxISOIEC 42005 Revolutionalises AI Impact Assessment.pptx
ISOIEC 42005 Revolutionalises AI Impact Assessment.pptx
AyilurRamnath1
 
End-to-end Assurance for SD-WAN & SASE with ThousandEyes
End-to-end Assurance for SD-WAN & SASE with ThousandEyesEnd-to-end Assurance for SD-WAN & SASE with ThousandEyes
End-to-end Assurance for SD-WAN & SASE with ThousandEyes
ThousandEyes
 
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to KnowWhat is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
SMACT Works
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy SurveyTrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc
 
Trends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary MeekerTrends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary Meeker
Clive Dickens
 
Domino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use CasesDomino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use Cases
panagenda
 
Soulmaite review - Find Real AI soulmate review
Soulmaite review - Find Real AI soulmate reviewSoulmaite review - Find Real AI soulmate review
Soulmaite review - Find Real AI soulmate review
Soulmaite
 
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und AnwendungsfälleDomino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
panagenda
 
“How Qualcomm Is Powering AI-driven Multimedia at the Edge,” a Presentation f...
“How Qualcomm Is Powering AI-driven Multimedia at the Edge,” a Presentation f...“How Qualcomm Is Powering AI-driven Multimedia at the Edge,” a Presentation f...
“How Qualcomm Is Powering AI-driven Multimedia at the Edge,” a Presentation f...
Edge AI and Vision Alliance
 
If You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FMEIf You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FME
Safe Software
 
Improving Developer Productivity With DORA, SPACE, and DevEx
Improving Developer Productivity With DORA, SPACE, and DevExImproving Developer Productivity With DORA, SPACE, and DevEx
Improving Developer Productivity With DORA, SPACE, and DevEx
Justin Reock
 
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and ImplementationAI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
Christine Shepherd
 
6th Power Grid Model Meetup - 21 May 2025
6th Power Grid Model Meetup - 21 May 20256th Power Grid Model Meetup - 21 May 2025
6th Power Grid Model Meetup - 21 May 2025
DanBrown980551
 
FCF- Getting Started in Cybersecurity 3.0
FCF- Getting Started in Cybersecurity 3.0FCF- Getting Started in Cybersecurity 3.0
FCF- Getting Started in Cybersecurity 3.0
RodrigoMori7
 
7 Salesforce Data Cloud Best Practices.pdf
7 Salesforce Data Cloud Best Practices.pdf7 Salesforce Data Cloud Best Practices.pdf
7 Salesforce Data Cloud Best Practices.pdf
Minuscule Technologies
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean accountYour startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
Oracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI FoundationsOracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI Foundations
VICTOR MAESTRE RAMIREZ
 
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Safe Software
 
Jira Administration Training – Day 1 : Introduction
Jira Administration Training – Day 1 : IntroductionJira Administration Training – Day 1 : Introduction
Jira Administration Training – Day 1 : Introduction
Ravi Teja
 
ISOIEC 42005 Revolutionalises AI Impact Assessment.pptx
ISOIEC 42005 Revolutionalises AI Impact Assessment.pptxISOIEC 42005 Revolutionalises AI Impact Assessment.pptx
ISOIEC 42005 Revolutionalises AI Impact Assessment.pptx
AyilurRamnath1
 
End-to-end Assurance for SD-WAN & SASE with ThousandEyes
End-to-end Assurance for SD-WAN & SASE with ThousandEyesEnd-to-end Assurance for SD-WAN & SASE with ThousandEyes
End-to-end Assurance for SD-WAN & SASE with ThousandEyes
ThousandEyes
 
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to KnowWhat is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
SMACT Works
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy SurveyTrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc
 
Trends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary MeekerTrends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary Meeker
Clive Dickens
 
Domino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use CasesDomino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use Cases
panagenda
 
Soulmaite review - Find Real AI soulmate review
Soulmaite review - Find Real AI soulmate reviewSoulmaite review - Find Real AI soulmate review
Soulmaite review - Find Real AI soulmate review
Soulmaite
 
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und AnwendungsfälleDomino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
panagenda
 
“How Qualcomm Is Powering AI-driven Multimedia at the Edge,” a Presentation f...
“How Qualcomm Is Powering AI-driven Multimedia at the Edge,” a Presentation f...“How Qualcomm Is Powering AI-driven Multimedia at the Edge,” a Presentation f...
“How Qualcomm Is Powering AI-driven Multimedia at the Edge,” a Presentation f...
Edge AI and Vision Alliance
 
If You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FMEIf You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FME
Safe Software
 
Improving Developer Productivity With DORA, SPACE, and DevEx
Improving Developer Productivity With DORA, SPACE, and DevExImproving Developer Productivity With DORA, SPACE, and DevEx
Improving Developer Productivity With DORA, SPACE, and DevEx
Justin Reock
 
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and ImplementationAI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
Christine Shepherd
 
6th Power Grid Model Meetup - 21 May 2025
6th Power Grid Model Meetup - 21 May 20256th Power Grid Model Meetup - 21 May 2025
6th Power Grid Model Meetup - 21 May 2025
DanBrown980551
 
FCF- Getting Started in Cybersecurity 3.0
FCF- Getting Started in Cybersecurity 3.0FCF- Getting Started in Cybersecurity 3.0
FCF- Getting Started in Cybersecurity 3.0
RodrigoMori7
 
7 Salesforce Data Cloud Best Practices.pdf
7 Salesforce Data Cloud Best Practices.pdf7 Salesforce Data Cloud Best Practices.pdf
7 Salesforce Data Cloud Best Practices.pdf
Minuscule Technologies
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean accountYour startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 

Python Programming - IX. On Randomness

  • 1. IX. ON RANDOMNESS Engr. RANEL O. PADON
  • 2. PYTHON PROGRAMMING TOPICS I •Introduction to Python Programming II •Python Basics III •Controlling the Program Flow IV •Program Components: Functions, Classes, Packages, and Modules V •Sequences (List and Tuples), and Dictionaries VI •Object-Based Programming: Classes and Objects VII •Customizing Classes and Operator Overloading VIII •Object-Oriented Programming: Inheritance and Polymorphism IX •Randomization Algorithms X •Exception Handling and Assertions XI •String Manipulation and Regular Expressions XII •File Handling and Processing XIII •GUI Programming Using Tkinter
  • 5. ON RANDOMNESS Randomization Algorithms * Random numbers are used for testing the performance of programs, creating scientific simulations, and so on. * A pseudorandom number generator (PRNG) is an algorithm for generating a sequence of numbers that approximates the properties of random numbers. * The sequence is not truly random in that it is completely determined by a relatively small set of initial values (random seed)
  • 6. ON RANDOMNESS Randomization Algorithms Early Algorithm Middle-Square Method * take any number, square it, remove the middle digits of the resulting number as the "random number", then use that number as the seed for the next iteration. * squaring the number "1111" yields "1234321", which can be written as "01234321", an 8-digit number being the square of a 4-digit number. This gives "2343" as the "random" number. Repeating this procedure gives "4896" as the next result, and so on.
  • 7. ON RANDOMNESS Randomization Algorithms 1.) Blum Blum Shub (cryptographically sound, but slow) 2.) Mersenne Twister - fast with good statistical properties, - used when cyptography is not an issue - used in Python 3.) Linear Congruential Generator - commonly-used in compilers and many, many others
  • 8. ON RANDOMNESS Randomization Algorithms 2.) Mersenne Twister Algorithm is based on a matrix linear recurrence over a finite binary field provides for fast generation of very high-quality pseudorandom numbers, having been designed specifically to rectify many of the flaws found in older algorithms. used in PHP, Ruby and Python name derives from the fact that period length is chosen to be a Mersenne prime
  • 9. ON RANDOMNESS Mersenne Twister Algorithm 2.) Mersenne Twister Algorithm It has a very long period of 219937 − 1. While a long period is not a guarantee of quality in a random number generator, short periods (such as the 232 common in many software packages) can be problematic. It is k-distributed to 32-bit accuracy for every 1 ≤ k ≤ 623 It passes numerous tests for statistical randomness, including the Diehard tests. It passes most, but not all, of the even more stringent TestU01 Crush randomness tests.
  • 10. ON RANDOMNESS Mersenne Twister Algorithm 2.) Mersenne Twister Algorithm (as used in the random module of Python) # Create a length 624 list to store the state of the generator MT = [0 for i in xrange(624)] index = 0 # To get last 32 bits bitmask_1 = (2 ** 32) - 1 # To get 32. bit bitmask_2 = 2 ** 31 # To get last 31 bits bitmask_3 = (2 ** 31) - 1
  • 11. ON RANDOMNESS Mersenne Twister Algorithm 2.) Mersenne Twister Algorithm def initialize_generator(seed): "Initialize the generator from a seed" global MT global bitmask_1 MT[0] = seed for i in xrange(1,624): MT[i] = ((1812433253 * MT[i-1]) ^ ((MT[i-1] >> 30) + i)) & bitmask_1
  • 12. ON RANDOMNESS Mersenne Twister Algorithm def extract_number(): ""“ Extract a tempered pseudorandom number based on the indexth value, calling generate_numbers() every 624 numbers ""“ global index global MT if index == 0: generate_numbers() y = MT[index] y ^= y >> 11 y ^= (y << 7) & 2636928640 y ^= (y << 15) & 4022730752 y ^= y >> 18 index = (index + 1) % 624 return y
  • 13. ON RANDOMNESS Mersenne Twister Algorithm def generate_numbers(): "Generate an array of 624 untempered numbers" global MT for i in xrange(624): y = (MT[i] & bitmask_2) + (MT[(i + 1 ) % 624] & bitmask_3) MT[i] = MT[(i + 397) % 624] ^ (y >> 1) if y % 2 != 0: MT[i] ^= 2567483615 if __name__ == "__main__": from datetime import datetime now = datetime.now() initialize_generator(now.microsecond) for i in xrange(100): "Print 100 random numbers as an example" print extract_number()
  • 14. ON RANDOMNESS Linear Congruential Generator 3.) Linear Congruential Generator represents one of the oldest and best-known pseudorandom number generator algorithms. the theory behind them is easy to understand, and they are easily implemented and fast.
  • 15. ON RANDOMNESS Linear Congruential Generator 3.) Linear Congruential Generator
  • 16. ON RANDOMNESS Randomization Algorithms 3.) Linear Congruential Generator The basic idea is to multiply the last number with a factor a, add a constant c and then modulate it by m. Xn+1 = (aXn + c) mod m. where X0 is the seed.
  • 17. ON RANDOMNESS Random Seed Random Seed * a number (or vector) used to initialize a pseudorandom number generator * crucial in the field of computer security. * having the seed will allow one to obtain the secret encryption key in a pseudorandomly generated encryption values
  • 18. ON RANDOMNESS Random Seed Random Seed * two or more systems using matching pseudorandom number algorithms and matching seeds can generate matching sequences of non-repeating numbers which can be used to synchronize remote systems, such as GPS satellites and receivers.
  • 19. ON RANDOMNESS Linear Congruential Generator a=3 c=9 m = 16 xi = 0 def seed(x): global xi xi = x Random Number Generator def rng(): global xi xi = (a*xi + c) % m return xi for i in range(10): print rng()
  • 20. ON RANDOMNESS Linear Congruential Generator LCG (Standard Parameters) Good One
  • 21. ON RANDOMNESS Linear Congruential Generator Using java.util.Random parameters a = 25214903917 c = 11 m = 2**48 xi = 1000 def seed(x): global xi xi = x def rng(): global xi xi = (a*xi + c) % m return xi def rng_bounded(low, high): return low + rng()%(high - low+1) for i in range(10): rng_bounded(1, 10)
  • 22. ON RANDOMNESS Minimal Standard MINSTD by Park & Miller (1988) known as the Minimal Standard Random number generator a very good set of parameters for LCG: m = 231 − 1 = 2,147,483,647 (a Mersenne prime M31) a = 75 = 16,807 (a primitive root modulo M31) c=0 often the generator that used for the built in random number function in compilers and other software packages. used in Apple CarbonLib, a procedural API for developing Mac OS X applications
  • 23. ON RANDOMNESS Linear Congruential Generator Using MINSTD parameters a = 7**5 c=0 m = 2**31 - 1 xi = 1000 def seed(x): global xi xi = x def rng(): global xi xi = (a*xi + c) % m return xi def rng_bounded(low, high): return low + rng() % (high - low+1) for i in range(10): rng_bounded(1, 2)
  • 24. ON RANDOMNESS Linear Congruential Generator Tossing 1 Coin using LCG MINSTD (1 million times) from __ future__ import division if __name__ == '__main__': main() a = 7**5 c=0 m = 2**31 - 1 xi = 1000 def seed(x): global xi xi = x def rng(): global xi xi = (a*xi + c) % m return xi
  • 25. ON RANDOMNESS Linear Congruential Generator Tossing 1 Coin using LCG MINSTD (1 million times) def rng_bounded(low, high): return low + rng() % (high - low+1) (heads, tails, count) = (0, 0, 0) for i in range (1, 1000001): count += 1 coin rng_bounded(1,2) if coin == 1: heads += 1 else: tails += 1
  • 26. ON RANDOMNESS Linear Congruential Generator Tossing 1 Coin using LCG MINSTD (1 million times) print "heads is ", heads/count print "tails is ", tails/count
  • 27. ON RANDOMNESS Linear Congruential Generator Tossing 1 Coin using Python randrange() (1 million times) from __ future__ import division import random if __name__ == '__main__': main() (heads, tails, count) = (0, 0, 0) random.seed(1000) for i in range (1,1000001): count +=1 coin = random.randrange(1,3)
  • 28. ON RANDOMNESS Linear Congruential Generator Tossing 1 Coin using Python randrange() (1 million times) if coin == 1: heads += 1 else: tails += 1 print "heads is ", heads/count print "tails is ", tails/count
  • 29. ON RANDOMNESS Linear Congruential Generator Tossing 2 Coins using LCG MINSTD (1 million times) from __ future__ import division if __name__ == '__main__': main() a = 7**5 c=0 m = 2**31 - 1 xi = 1000 def seed(x): global xi xi = x def rng(): global xi xi = (a*xi + c) % m return xi
  • 30. ON RANDOMNESS Linear Congruential Generator Tossing 2 Coins using LCG MINSTD (1 million times) def rng_bounded(low, high): return low + rng() % (high - low+1) (heads, tails, combo, count) = (0, 0, 0, 0) for i in range (1,1000001): count += 1 coin1 = rng_bounded(1,2) coin2 = rng_bounded(1,2) sum = coin1 + coin2 if sum == 2: heads += 1 elif sum == 4: tails += 1 else: combo += 1
  • 31. ON RANDOMNESS Linear Congruential Generator Tossing 2 Coins using LCG MINSTD (1 million times) print "head is ", heads/count print "tail is ", tails/count print "combo is ", combo/count
  • 32. ON RANDOMNESS Linear Congruential Generator Tossing 2 Coins using Python randrange() (1 million times) from __ future__ import division import random if __name__ == '__main__': main() (heads, tails, combo, count) = (0, 0, 0, 0) random.seed(1000) for i in range (1,1000001): count +=1 coin1 = random.randrange(1,3) coin2 = random.randrange(1,3) sum = coin1 + coin2
  • 33. ON RANDOMNESS Linear Congruential Generator Tossing 2 Coins using Python randrange() (1 million times) if sum == 2: heads += 1 elif sum == 4: tails += 1 else: combo += 1 print "head is ", heads/count print "tail is ", tails/count print "combo is ", combo/count
  • 34. ON RANDOMNESS Results Summary Almost Similar Results Tossing 1 Coin using LCG MINSTD vs Python randrange() (1 million times) Tossing 2 Coins using LCG MINSTD vs Python randrange() (1 million times)
  • 35. ON RANDOMNESS Linear Congruential Generator Tossing 1 Coin using LCG MINSTD (1000 times) More Elegant Way (Using List)
  • 36. ON RANDOMNESS Linear Congruential Generator Tossing 2 Coins using LCG MINSTD (1 million times) More Elegant Way (Using List) from __future__ import division if __name__ == '__main__': main() xi = 1000 def seed(x): global xi xi = x def rng(a=7**5, c=0, m = 2**31 - 1): global xi xi = (a*xi + c) % m return xi def rng_bounded(low, high): return low + rng() % (high - low+1)
  • 37. ON RANDOMNESS Linear Congruential Generator Tossing 2 Coins using LCG MINSTD (1 million times) More Elegant Way tosses = [] for i in range (1, 1000001): tosses += [rngNiRanie(1, 2) + rngNiRanie(1, 2)] print "heads count is ", tosses.count(2) / len(tosses) print "tails count is ", tosses.count(4) / len(tosses) print "combo count is ", tosses.count(3) / len(tosses)
  • 39. ON RANDOMNESS a = 25214903917 c = 11 m = 2**48 xi = 1000 def seed(x): global xi xi = x def rng(): global xi xi = (a*xi + c) % m return xi def ranie(lowerBound, upperBound): #ano laman nito? # for i in range(10): ranie(1,10)
  • 40. REFERENCES  Deitel, Deitel, Liperi, and Wiedermann - Python: How to Program (2001).  Disclaimer: Most of the images/information used here have no proper source citation, and I do not claim ownership of these either. I don’t want to reinvent the wheel, and I just want to reuse and reintegrate materials that I think are useful or cool, then present them in another light, form, or perspective. Moreover, the images/information here are mainly used for illustration/educational purposes only, in the spirit of openness of data, spreading light, and empowering people with knowledge. 