Random number generation on 8 bit MCU

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

Random number generation on 8 bit MCU

978 次查看
Skaptor
Contributor I

Hello,

 

I am currently trying to find a solution for random number, my program will randomize a list of guys that have to do certain task.

 

We are 6 people, so the solution i thought was:

 

rand_person = TPM1CNT % 6 so the output will give me 0,1,2,3,4,5. But it isn´t working as espected.

 

also the TPM1MOD = 655; so it interrupts every 10 ms

 

So i will be pleased if someone could tell me the best way to generate a random number.

 

regards

标签 (1)
0 项奖励
回复
4 回复数

592 次查看
bigmac
Specialist III

Hello,

 

You did not say in what manner the result was not as expected.

 

Your method requires that the samples occur at a random times, relative to each other, and to the MCU start up, otherwise the result will not be random.  Since there are 656 states (0-655) for TPM1, this is not evenly divisible by 6.  So the result will be slightly skewed to the results 0 and 1

 

I would expect that the sample would need to be externally triggered by some means.

 

Here are a couple of earlier threads about random number generation methods.

https://community.freescale.com/message/24955#24955

https://community.freescale.com/message/45987#45987

 

Regards,

Mac

 

0 项奖励
回复

592 次查看
Skaptor
Contributor I

Hello,

 

As for the result not being expected, the reason is almost every time the same guy appears.

 

I though the operator "%" gives you the residue of the division of the number you tell?.

 

I´ve searched for a non-gaussian way of getting random numbers.

 

I´ve also used the CRC algorithm you suggested in the first thread but I encountered a problem the numbers are always the same, do I need to change the SEED value trigger every time I want a different number (maybe with a timer?)?

 

regards,

JC

0 项奖励
回复

592 次查看
bigmac
Specialist III

Hello JC,

 

Your are correct about the % operator.  If the same result keeps repeating, this would seem to indicated that the timing of each result is not random, but is related to the timer you are using to determine the result.

 

Manual pushbutton operation should give random timing, provided you are not using the timer to debounce the pushbutton.

 

It appears that you may be attempting to "throw" a die.

 

The CRC algorithm is probably not suited to this application, since the sequence is fixed and predictable, though with some of the properties of a random number.  The seed value will determine the starting point within the sequence.  For a 16-bit CRC, the sequence length would be 65535 (2^16 - 1), beyond which the same sequence would repeat.

 

Regards,

Mac

0 项奖励
回复

592 次查看
Skaptor
Contributor I

Hello,

 

Sorry for not responding in a while..

 

I left the program as it was originally, the sequence "randomness" its actually the time gap that you use for pressing the generate button.

 

Thanks for your help.

 

Regards

0 项奖励
回复