• AVR Freaks

Hot!Strange Code generated with XC8

Page: < 12 Showing page 2 of 2
Author
NorthGuy
Super Member
  • Total Posts : 6350
  • Reward points : 0
  • Joined: 2014/02/23 14:23:23
  • Location: Northern Canada
  • Status: offline
Re: Strange Code generated with XC8 2019/12/13 08:28:57 (permalink)
+1 (1)
1and0
    Zahl = rand() % 6 + 1;

or this with a free-running timer
    Zahl = TMR0L % 6 + 1;




This will produce biased probabilities. The probability to draw 1, 2, or 3 is 43/256 = 0.168, but probability to draw 4, 5, o 6 is only 42/256 = 0.164.
#21
1and0
Access is Denied
  • Total Posts : 11337
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Strange Code generated with XC8 2019/12/13 08:50:34 (permalink)
0
For good sources of randomness:
Also, have a read of this: https://hackaday.com/2015...tor-for-a-true-hacker/
post edited by 1and0 - 2019/12/13 08:56:08
#22
1and0
Access is Denied
  • Total Posts : 11337
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Strange Code generated with XC8 2019/12/13 09:03:54 (permalink)
+1 (1)
#23
1and0
Access is Denied
  • Total Posts : 11337
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Strange Code generated with XC8 2019/12/13 09:23:54 (permalink)
+1 (1)
NorthGuy
This will produce biased probabilities. The probability to draw 1, 2, or 3 is 43/256 = 0.168, but probability to draw 4, 5, or 6 is only 42/256 = 0.164.

My probability is a little rusty, but I think 4 has a probability of 43/256. ;)
 
Edit: Using the 16-bit timer:
    Zahl = TMR0 % 6 + 1;

should yield probability of 0.166672 for 1, 2, 3, and 4, and probability of 0.166656 for 5 and 6. ;)
 
post edited by 1and0 - 2019/12/13 09:34:08
#24
NorthGuy
Super Member
  • Total Posts : 6350
  • Reward points : 0
  • Joined: 2014/02/23 14:23:23
  • Location: Northern Canada
  • Status: offline
Re: Strange Code generated with XC8 2019/12/13 09:34:38 (permalink)
0
1and0
NorthGuy
This will produce biased probabilities. The probability to draw 1, 2, or 3 is 43/256 = 0.168, but probability to draw 4, 5, or 6 is only 42/256 = 0.164.

My probability is a little rusty, but I think 4 has a probability of 43/256. ;)

 
Yes
#25
NorthGuy
Super Member
  • Total Posts : 6350
  • Reward points : 0
  • Joined: 2014/02/23 14:23:23
  • Location: Northern Canada
  • Status: offline
Re: Strange Code generated with XC8 2019/12/13 09:37:21 (permalink)
0
1and0
Edit: Using the 16-bit timer:



The best way is to use a timer with PR register and set the PR register to 5. This way the probabilities will be exact unless the sample time is periodic.
#26
1and0
Access is Denied
  • Total Posts : 11337
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Strange Code generated with XC8 2019/12/13 09:47:46 (permalink)
0
NorthGuy
 
The best way is to use a timer with PR register and set the PR register to 5. This way the probabilities will be exact unless the sample time is periodic.

Good idea. Sample it when the button is pressed.
#27
Page: < 12 Showing page 2 of 2
Jump to:
© 2020 APG vNext Commercial Version 4.5