# Random number generator, algorithm question.

Status
Not open for further replies.

#### zuzu

##### Member level 3
Hello friends,

Trying to distribute packet error over time in some protocol, I need to generate a custom (pseudo) random sequence up to certain number.
I my MCU I have only rand() and srand(int seed) functions.

But I need to generate a PR number from 0 to N where N can be anywhere up to 2047. The seed is not important, but non-repetitive values are, and cycle them all up to N.

Any idea appreciated.

#### vinoth8051

##### Full Member level 3
Hi Zuzu,
Pseudo random sequence can be generated by using shift-registers with the XNOR feed back. kindly check these links.
Quick 8-Bit Random Number Generators (PIC ASM)

#### zorro

Hi zuzu,

If you rand() generator gives random numbers in 16 bits, and you need numbers ranging from 0 to 2047 (i.e. 11 bits) you can call rand() function and take the 11 least significant bits of the value it returns.
This does not give a cycle of 2048 values, but longer (with pseudo-random repetitions). Is it really important that all values don't repeat before a whole cycle is completed?
Regards

Z

Last edited:

#### FvM

##### Super Moderator
Staff member
I'm not aware of algorithm to generate a 0 to N PR sequence. Popular LFSR (Linear Feedback Shift Registers) generators have fixed 2^n -1 sequence length, all numbers of a binary range excluding 0.

As a simple straightforward solution, I would use a 12 Bit LFSR generator, map N to 0 and drop numbers > N. If you are required to supply a new number every clock cycle, use a precalculated table.

#### zuzu

##### Member level 3

I think I need to detail some more, maybe another idea comes up .

I have a unidirectional (no reverse link, like sat downlink) transmission of packets over a (quite susceptible) noisy channel. Number of packets in burst can vary up to 2048 and now, the burst is implemented to send linear, from 0 -> N. Now the receiver can catch all N or may loose one and in real life, if one bit is lost, CRC will reject entire packet. Note: FEC is impractical at the moment due to computing limmitations which wasn't encountered in this hw.

So the chance for receiver to catch next lost is at next burst, at minimum distance N*tpacket time. If on next burst packets are in different order (aka random up to N) the time to catch lost packet is probably reduced.

#### zorro

Hi zuzu,

Sorry, my question can be silly. But if I understant you, if in the next burst packets are in different order, then the time to catch lost packet(s) could be reduced or increased. Right? Is the expected (mean) time reduced?
Regards

Z

#### FvM

##### Super Moderator
Staff member
In most test generator applications PR numbers are used to get a deterministic random like data stream, that can be compared against the expected signal at the receiver. You apparently intend something different, but the specification isn't clear.

Status
Not open for further replies.