Type a keystroke randomly

I have read some posts about using the random function but I can't find how I would go about implementing a way to randomly type a keystroke.

In other words. I'd like for it to sometimes type it and sometimes not (randomly). I imagine I could tell it to generate a random number from 0 to 9. Then compare. If the generated number is say less than X (whatever number I set) do something.

Possible?

I think your proposed method sounds reasonable, and you can come up with any number of concoctions or formulae to govern the likelihood of a key being pressed. Here's one example:

If you needed the choice of keystroke itself to be random, then I think you'd need a bit of scripting. Here's some AppleScript that does the same thing as above, but also randomises the key that's pressed:

use application "System Events"
use scripting additions

repeat until (random number 100) mod 15 = 0
	key code (random number 47) -- 0 to 47 are the typable keys on the keyboard
	
	delay (random number 10) -- delay in seconds
end repeat