Random Numbers - PowerPoint PPT Presentation

About This Presentation
Title:

Random Numbers

Description:

... of of a computer word with the radix point assumed at the far-right. Un can be regarded as the contents of the same word with the radix point at the far-left. ... – PowerPoint PPT presentation

Number of Views:31
Avg rating:3.0/5.0
Slides: 7
Provided by: Parik
Category:
Tags: numbers | radix | random

less

Transcript and Presenter's Notes

Title: Random Numbers


1
Random Numbers
  • Dick Steflik

2
Pseudo Random Numbers
  • In most cases we do not want truly random numbers
  • most applications need the idea of repeatability
    to be able to debug
  • if we used truly random numbers how would we be
    able to debug a program, every time we would run
    the program it would be a different problem
  • What we really want is something that will appear
    to be random but will be able to reproduce a
    sequence

3
Generation Uniform Random Numbers
  • Consider a methos for generating a sequence of
    random fractions (Random real numbers ) Un
  • uniformly distributed between 0 and 1
  • Since a computer can only represent a real number
    with finite accuracy well actually generate
    integers Xn between 0 and some number m
  • The fraction Un Xn / m
  • will always be between 0 and 1
  • Usually m is the word size of the computer so
    that Xn can be regarded as the integer content of
    of a computer word with the radix point assumed
    at the far-right.
  • Un can be regarded as the contents of the same
    word with the radix point at the far-left.

4
The Linear Congruential Method
By far the most popular random number generators
in use today are special cases of the following
scheme, introduced by D.H.Lehmer in 1949.
Choose 4 magic numbers m, the modulus m gt
0 a, the multiplier 0 lt a lt m c, the
increment 0 lt c lt m X0 the Starting value 0
lt Xo lt m
The desired sequence is then Xn1 (aXn c)
mod m, n gt 0
This is called a linear congruential sequence
5
Example
For m 10 X0 a c 7 the sequence is 7, 6,
9, 0, 7, 6, 9, 0 notice that the sequence has
a period of 4 I.e. it repeats and will do so
forever. This will happen for any linear
congruential generator.
6
Sample
include ltiostream.hgtint seed int random() int
num (13seed11) 11 seed num return
num int main() cout ltlt "input a seed
number " cin gtgt seed cout ltlt "\n"
for int j 0 j lt 20 j) cout ltlt
random() ltlt " "
Write a Comment
User Comments (0)
About PowerShow.com