Using random numbers - PowerPoint PPT Presentation

About This Presentation
Title:

Using random numbers

Description:

Automating pattern creation. Use these standard runtime functions; #include stdlib.h ... But need a larger pattern-bitmap. Example: use 2 bits-per-pixel (4 colors) ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 19
Provided by: allan96
Learn more at: https://www.cs.usfca.edu
Category:
Tags: numbers | random | using

less

Transcript and Presenter's Notes

Title: Using random numbers


1
Using random numbers
  • Some ways the standard UNIX rand()
    library-function can be deployed to generate
    graphics and sound

2
Automating pattern creation
  • Use these standard runtime functions
  • include ltstdlib.hgt
  • int rand( void )
  • void srand( unsigned int seed )
  • Make a new 8x8 bitmap pattern like this
  • unsigned char pat 8
  • for (k 0 k lt 8 k) pat k rand()
  • fgcolor rand() bgcolor rand()

3
Esthetics
  • Use a brighter color in the foreground
  • Use a darker color in the background
  • To implement this discipline we need to know how
    the color-table is arranged
  • In mode 19 there are 256 default colors
  • Among these are 24 color-groups
  • 3 intensity-levels plus 3 saturation-levels

4
The default colors (mode 19)
  • Range for the 72 brightest colors 32103
  • Range for the 72 midlevel colors 104-175
  • Range for the 72 darkest colors 176-247
  • Colors 0-15 are the standard EGA colors
  • Colors 16-31 are sixteen grayscale colors
  • Colors 248-255 are solid black (default)
  • (But all of these are fully programmable)

5
Choosing a random color-pair
  • foreground color (from the bright range)
  • fgcolor ( ( rand() 0xFF ) 72 ) 32
  • Background color (from the dark range)
  • bgcolor ( ( rand() 0xFF ) 72 ) 104

6
Using patterns with more colors
  • Same concepts can be extended
  • But need a larger pattern-bitmap
  • Example use 2 bits-per-pixel (4 colors)
  • An 8x8 pattern that using 4 colors
  • unsigned short pat2bpp 8
  • unsigned char palette4 4
  • for (r 0 r lt 8 r) pat2bpp r rand()
  • for (c 0 c lt 4 c) palette4 c rand()

7
Tiling with a 4-color bitmap
  • for (y 0 y lt hres y)
  • unsigned short bits pat2bpp y 8
  • for (x 0 x lt hres x)
  • int i ( bits gtgt ( x 8 )3
  • int color palette4 i
  • vram yhres x color

8
Automating an art show
  • Can use a standard C runtime function
  • include ltunistd.hgt
  • void sleep( int seconds )
  • User views your screen for fixed duration
  • while ( !done )
  • draw_next_scene() sleep(1)

9
In-class exercise 1
  • Can you use the UNIX random-number generator
    function to create an art show (i.e., a
    succession of esthetically pleasing patterns that
    tile the display screen for a timed duration)?

10
Generating white noise
  • We can use the rand() function to create
    pulse-code data for a Waveform Audio file
  • When we play that .wav file what we will hear is
    a sound known as white noise
  • Lets look at the details for IBM/Microsoft
    Waveform Audio file-format (.wav) their
    original simple version (Version 1.0)
  • Its contents is organized into chunks

11
The Waveform Audio-File Format
RIFF Chunk (12 bytes)
FORMAT Chunk (24 bytes)
DATA Chunk (size varies)
Version 1.0
Developed jointly in 1991 by IBM and Microsoft
Corporation for the Windows 3.1
operating system
12
The RIFF chunk
RIFF Resource Interchange File Format
12 bytes
RIFF
Total number of the files bytes that follow
WAVE
chunkName chunkSize
chunkType (ascii characters)
(unsigned int) (ascii characters)
Remainder of the RIFF chunk holds all the files
other chunks
13
The FORMAT chunk
fmt (ascii characters)
Number of chunks bytes that follow (i.e., 16)
samplesPerSec (e.g., 44100)
formatTag (i.e., 0 or 1)
nChannels (mono1 or stereo2)
avBytesPerSec
frameAlignmt (i.e., 1, 2, 4)
bitsPerSample (e.g., 8 or 16)
Originally was a fixed size header (8 bytes)
plus parameters (16 bytes)
TOTAL SIZE 24 bytes
14
The DATA chunk
data (ascii characters)
Number of chunks bytes that follow (i.e., 16)
The PCM data goes here (Pulse Code
Modulation) size is variable
15
8-Bit Sample Formats
  • 8-bit monoaural Each sample is one byte
  • Value is an unsigned char in range 0..255
  • The neutral value is 128 (i.e., silence)
  • 8-bit stereo Each sample is a byte-pair
  • First sample-value is for left-hand channel,
    second sample-value is right-hand channel

16
16-Bit Sample Formats
  • 16-bit monoaural sample-size is two bytes
  • Value is a short in range -32768..32767
  • The default storage-convention is Big-Endian
  • The neutral value is 0 (i.e., silence)
  • 16-bit stereo sample-size is four bytes
  • First sample-value is for left-hand channel,
    second sample-value is right-hand channel

17
Our mknoises.cpp demo
  • Program creates a file named noises.wav
  • It demonstrates stereophonic white noise
  • First the left-hand channel plays white noise
  • Then right-hand channel plays white noise
  • Uses 8-bit stereo sample-format

Left-channel pulse-code
Right-channel pulse-code
Each sample stores a pair of 8-bit pulse-code
values
18
In-class exercise 2
  • Can you modify the mknoises.cpp demo so that
    noise from the left-hand channel gradually
    diminishes in volume while the noise from the
    right-channel is gradually getting louder?
Write a Comment
User Comments (0)
About PowerShow.com