User Tools

Site Tools


helps_and_suggestions

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
helps_and_suggestions [2019/04/10 11:39]
hutch created
helps_and_suggestions [2019/04/10 11:40]
hutch [Helps: Enabling and disabling test code using ''#ifdef'' and ''#endif'']
Line 1: Line 1:
 ===== Helps and Suggestions ===== ===== Helps and Suggestions =====
  
-==== Helps: ​Generating Random Sequences ====+==== Generating Random Sequences ====
 If you include "​stdlib.h"​ you have access to two functions that help to generate random sequences. The first function ''​srand(seed)''​ allows you to provide a "​seed"​ value for the random number generator. Each time you call ''​rand()'',​ it will return an integer that is part of a pseudorandom sequence. However, each time you start your program from scratch, ''​rand()''​ will return the same sequence of integers. If, each time you run the program, you start out by invoking ''​srand(seed)''​ with a different seed value, the sequence will be different each time you run the program. So, how do you generate random seed values? In my Simon game I am incrementing a counter while waiting for the user to touch the screen. Once the user touches the screen, I take the counter value and use that as the seed value for ''​srand()''​. If you include "​stdlib.h"​ you have access to two functions that help to generate random sequences. The first function ''​srand(seed)''​ allows you to provide a "​seed"​ value for the random number generator. Each time you call ''​rand()'',​ it will return an integer that is part of a pseudorandom sequence. However, each time you start your program from scratch, ''​rand()''​ will return the same sequence of integers. If, each time you run the program, you start out by invoking ''​srand(seed)''​ with a different seed value, the sequence will be different each time you run the program. So, how do you generate random seed values? In my Simon game I am incrementing a counter while waiting for the user to touch the screen. Once the user touches the screen, I take the counter value and use that as the seed value for ''​srand()''​.
  
Line 9: Line 9:
 ---- ----
  
-==== Helps: ​Enabling and disabling test code using ''#​ifdef''​ and ''#​endif''​ ====+==== Enabling and disabling test code using ''#​ifdef''​ and ''#​endif''​ ====
 See the code below for an example of how to individually compile test code. This comes from my ''​simonMain.c''​ file. As currently configured, this code will run the ''​simonDisplay_runTest()''​. See the code below for an example of how to individually compile test code. This comes from my ''​simonMain.c''​ file. As currently configured, this code will run the ''​simonDisplay_runTest()''​.
 <code C> <code C>
Line 46: Line 46:
 ---- ----
  
-==== Helps: ​Using Function Pointers to Time the Duration of Individual Ticks ====+==== Using Function Pointers to Time the Duration of Individual Ticks ====
 I wrote a function called ''​tickTimer()''​ that made it relatively easy to measure how long various tick functions were taking. You may find it useful to debug timing issues. I'll provide the code here and you can modify it to suit you. Note that it uses a function-pointer so that you can pass in the pointer to a tick function so it can be invoked and timed. As currently written, it keeps track of which tick() function consumed the most time. I found it quite helpful while debugging my Simon game because I could see what state-machine was taking the most time at any point during the game. I wrote a function called ''​tickTimer()''​ that made it relatively easy to measure how long various tick functions were taking. You may find it useful to debug timing issues. I'll provide the code here and you can modify it to suit you. Note that it uses a function-pointer so that you can pass in the pointer to a tick function so it can be invoked and timed. As currently written, it keeps track of which tick() function consumed the most time. I found it quite helpful while debugging my Simon game because I could see what state-machine was taking the most time at any point during the game.
  
helps_and_suggestions.txt ยท Last modified: 2019/04/10 11:40 by hutch