CPS 196 week 12

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

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

View Full Document Right Arrow Icon
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
Background image of page 1

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

View Full DocumentRight Arrow Icon
To solve problem of predictable sequence, you have to “seed” the random number generator, only seed once function strand (15);
Background image of page 2
Background image of page 3

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

View Full DocumentRight Arrow Icon
Background image of page 4
Background image of page 5
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

Page1 / 5

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 Right Arrow Icon
Ask a homework question - tutors are online