Commit c058cb0
authored
Replace uniform mouse jitter with Gaussian-distributed delays (#228)
## Summary
- Replaces `rand.Intn(5) - 2` (uniform ±2ms) mouse movement jitter with
Gaussian-distributed delays
- Affects both `doMoveMouseSmooth` and `doDragMouseSmooth` code paths
- No change to average movement duration — only per-step variance
increases
## Problem
The uniform ±2ms jitter on smooth mouse movement delays produces
near-constant inter-event timing (~40ms ± 2ms). Real human mouse
movement has substantially more timing variance — humans accelerate,
decelerate, and have natural motor noise that creates variable intervals
between input events.
Measured inter-event intervals from current implementation:
```
[40.1, 40, 39, 40.2, 40.2, 40, 40.1, 39, 39.2, 40.1]
```
This is unnaturally uniform. Real human mouse input shows much wider
spread.
## Fix
New `gaussianDelay(meanMs, minMs)` function using Box-Muller transform:
- **Mean** = same as before (preserves total movement duration on
average)
- **Stddev** = 40% of mean (e.g., 10ms mean → 4ms stddev → delays range
~3-30ms)
- **Floor** = 3ms (prevents zero/negative delays)
- **Ceiling** = 3x mean (prevents rare extreme outliers from stalling
movement)
This produces inter-event timing that follows a bell curve instead of a
flat line, matching real human motor noise characteristics.
## Test plan
- [x] `TestGaussianDelay` — validates mean (within 15%), variance (>5),
floor (≥3ms), ceiling (≤3x mean)
- [x] `TestGaussianDelay_VarianceMuchHigherThanUniform` — confirms >3x
the old uniform variance
- [x] All existing tests pass
Made with [Cursor](https://cursor.com)
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> **Medium Risk**
> Changes core input timing behavior for `MoveMouse`/`DragMouse`, which
could alter perceived speed/smoothness or impact consumers that assume
consistent step delays. Logic is bounded/clamped and covered by new
statistical unit tests, limiting risk.
>
> **Overview**
> **Smooth mouse movement timing is now more human-like.** The uniform
±2ms per-step jitter used during smooth `MoveMouse` and smooth
`DragMouse` has been replaced with `gaussianDelay()` (Box–Muller),
clamped to a floor and 3× mean, to introduce realistic inter-event
variance.
>
> **Default step delay was increased from 10ms to 20ms** for both
API-side point calculation and
`mousetrajectory.GenerateMultiSegmentTrajectory`, affecting how many
trajectory points are generated when `duration_ms` is provided.
>
> Adds unit tests validating `gaussianDelay()` mean/variance bounds and
demonstrating higher variance vs the previous uniform jitter (including
a Welford velocity-variance simulation).
>
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
7e5c658. Bugbot is set up for automated
code reviews on this repo. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->1 parent 5a2b521 commit c058cb0
3 files changed
Lines changed: 159 additions & 17 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
116 | 116 | | |
117 | 117 | | |
118 | 118 | | |
119 | | - | |
| 119 | + | |
120 | 120 | | |
121 | 121 | | |
122 | 122 | | |
| |||
173 | 173 | | |
174 | 174 | | |
175 | 175 | | |
176 | | - | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
177 | 180 | | |
178 | 181 | | |
179 | 182 | | |
| |||
190 | 193 | | |
191 | 194 | | |
192 | 195 | | |
193 | | - | |
194 | | - | |
195 | | - | |
196 | | - | |
197 | | - | |
198 | | - | |
199 | | - | |
200 | | - | |
| 196 | + | |
| 197 | + | |
201 | 198 | | |
202 | 199 | | |
203 | 200 | | |
| |||
1233 | 1230 | | |
1234 | 1231 | | |
1235 | 1232 | | |
1236 | | - | |
1237 | | - | |
1238 | | - | |
1239 | | - | |
1240 | | - | |
1241 | | - | |
| 1233 | + | |
| 1234 | + | |
| 1235 | + | |
1242 | 1236 | | |
1243 | 1237 | | |
1244 | 1238 | | |
| |||
1254 | 1248 | | |
1255 | 1249 | | |
1256 | 1250 | | |
| 1251 | + | |
| 1252 | + | |
| 1253 | + | |
| 1254 | + | |
| 1255 | + | |
| 1256 | + | |
| 1257 | + | |
| 1258 | + | |
| 1259 | + | |
| 1260 | + | |
| 1261 | + | |
| 1262 | + | |
| 1263 | + | |
| 1264 | + | |
| 1265 | + | |
| 1266 | + | |
| 1267 | + | |
| 1268 | + | |
| 1269 | + | |
| 1270 | + | |
| 1271 | + | |
| 1272 | + | |
1257 | 1273 | | |
1258 | 1274 | | |
1259 | 1275 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
| 7 | + | |
6 | 8 | | |
7 | 9 | | |
8 | 10 | | |
| |||
168 | 170 | | |
169 | 171 | | |
170 | 172 | | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
171 | 297 | | |
172 | 298 | | |
173 | 299 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
61 | 61 | | |
62 | 62 | | |
63 | 63 | | |
64 | | - | |
| 64 | + | |
65 | 65 | | |
66 | 66 | | |
67 | 67 | | |
| |||
0 commit comments