CPS 196 week 12

# CPS 196 week 12 - tells rand to start at different place in...

This preview shows pages 1–5. Sign up to view the full content.

CPS 196 week 12 How would you simulate luck/chance? - Flip coin - Rolling dice - Shuffling a deck of cards and picking from a deck - each much have same chance In C programming: Pseudo random Number in in stdlib.h used in system cls (clear) Functions rand() - returns a pseudo random number between RAND_MAX - RAND_MAX is a constant is the maximum number returned by rand (), depends on architecture of program How function rand () works - there is a sequence of numbers 41 18467 6334 26500 19169 15724 11478 29358 - first time you call rand () you get the first number - next time you call you get the next number result is max number result is “random” runs 10 times displays first ten random, it’s the sequence it already has

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
To solve problem of predictable sequence, you have to “seed” the random number generator, only seed once function strand (15);

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: tells rand to start at different place in the sequence to get random number each time, need to change srand every time-most people use time-this function returns number of seconds since Jan 1, 1970 0:00:00 GMT-each time you run it changes because the seconds have changed Every time you call rand you get a number between 0 to 32767-translate to coins even is heads odds is tails-modulo (%) o gives you remainder after division o 4%2 = 0 o 5%2 = 1 o 10%2=0 o 15490%2=0 o 0= even o 1=odd Simpler way Generate a random # between 0 and 10 (11 numbers) modulo 11 Generate 2 to 9 (8 numbers) Mod by 8 0 1 2 3 4 5 6 7 mod by 8 plus 2 2 3 4 5 6 7 8 9 mod between 1 and 6 Final Project: Game, done in lab week after we get back Learn Pointer, no quizzes on it or after this one...
View Full Document

## CPS 196 week 12 - tells rand to start at different place in...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online