# \$random() is not synthesisable, so i want the alternate for that - verilog

This is a discussion on \$random() is not synthesisable, so i want the alternate for that - verilog ; I need to generate a random number between 1 to 40 . without using the \$random() function , how will i generate it? Heard that linear feedback shift register (LFSR) algorithm can be used for this. Can i know some ...

1. ## \$random() is not synthesisable, so i want the alternate for that

I need to generate a random number between 1 to 40 . without using the
\$random() function , how will i generate it?
Heard that linear feedback shift register (LFSR) algorithm can be used
for this.
Can i know some details regarding this?
Or any other means for this random generation.
later, i may need to change the value 40 to 50 or to 60, how can i do
that?

2. ## Re: \$random() is not synthesisable, so i want the alternate for that

LFSR can be used for "PSEUDO random number generator". It means that
the sequence will be the same for each initial value.
If you want to use REAL ramdom number generator you have to build it by

3. ## Re: \$random() is not synthesisable, so i want the alternate for that

Hi Michael,

Let me make it clear, you mean to say that, for a
perticular initial value, i get the random sequence which is constant.
is it?
This would be fine.

But the problem is, if i want to generate random numbers between 1 to 4
or 1 to 8 or 1 to 16 or for the powers of 2, it would be easy.

I want to generate a random number between 1 to 40.

How do i do this?

If i use flops and tap at some perticular points and do some operations
on it and append that bit at the left extreem point, I will get random
number generated, but if it exceeds the top value(40), then its a
problem.

i want a solution for this.

thanks,
Supritha

4. ## Re: \$random() is not synthesisable, so i want the alternate for that

<suprithaswamy@gmail.com> wrote in message
>I need to generate a random number between 1 to 40 . without using the
> \$random() function , how will i generate it?
> Heard that linear feedback shift register (LFSR) algorithm can be used
> for this.
> Can i know some details regarding this?
> Or any other means for this random generation.
> later, i may need to change the value 40 to 50 or to 60, how can i do
> that?

To understand "how random" you need your information, what's the
application? How often do you need validly random values generated?

5. ## Re: \$random() is not synthesisable, so i want the alternate for that

Hi John,

Let me narrate my problem in an eloberate manner,

1)A number should get generated between 1 and 40 .
2) exculding the above generated number, a number should get generated
between 1 and 40 .
3) excluding the above two generated numbers, a number should get
generated between 1 and 40 .
..............................

In this fashion, all the possible numbers should get generated, later
the cycle can repeat......

can i use LFSR ?

If so, how can i?

Any other solution is also acceptable.

regards,
Supritha

6. ## Re: \$random() is not synthesisable, so i want the alternate for that

suprithaswamy@gmail.com wrote:
> Hi John,
>
>
> Let me narrate my problem in an eloberate manner,
>
> 1)A number should get generated between 1 and 40 .
> 2) exculding the above generated number, a number should get generated
> between 1 and 40 .
> 3) excluding the above two generated numbers, a number should get
> generated between 1 and 40 .
> .............................
>
> In this fashion, all the possible numbers should get generated, later
> the cycle can repeat......
>
> can i use LFSR ?
>
> If so, how can i?
>
> Any other solution is also acceptable.
>
>
> regards,
> Supritha

If you're looking for a lottery picker, run a high speed counter that's
1-40 and stopped with the press of a button, then run the counter 1-39
for the press of the next button and add 1 if the count is greater than
the last number, and so on until the button-pushing user has randomly
selected the 6 winning lottery numbers.

There's no need to get fancy if the counter cycles 1000s of times a second.

7. ## Re: \$random() is not synthesisable, so i want the alternate for that

Hi John,

This is not the case i wanted, i want to implement in
hardware.
So, i prefer using flops and some of the gates, so i think this lottery
picker will not help me in this regard.
I prefer LFSR because it is hardware friendly.
so can you tell me some solution considering my concerns.

regards,
Supritha

8. ## Re: \$random() is not synthesisable, so i want the alternate for that

Dear Supritha ,

You could use something like that:

assign rng_number = (lfsr_out > 40) lfsr_out-40 : lfsr_out;

But again, LFSR is not the true RNG number.
If you use LFSR you should be very carefully with choosing the
configuration. Some configurations generate VERY short sequence. After
sequence is finished it will be started again and again.

9. ## Re: \$random() is not synthesisable, so i want the alternate for that

Thanks michael,

Thanks for your suggesion, its working.

But as a add on, i dont want the random number repeat within the
sequence length.

Can u tell something regarding this.

Regards,
Supritha

10. ## Re: \$random() is not synthesisable, so i want the alternate for that

Supritha,

You should look a little deeper into the workings of the LFSR.
It is possible to design LFSR's that are not maximal for a given
number of bits. Generally the maximal LFSR will have 2^n - 1
states for n bits, but with different feedback taps you can generate
other numbers of states. As far as I know, you need to experiment
to find the tap settings. I always got the tap settings for maximal
LFSR's from a table, but these tables were filled in by someone
who searched for them. I don't know of the existence of tables for
non-maximal LFSR's, but for one as short as 40 states it shouldn't
take too long to find one. Note that the 40 states you get in an
LFSR probably wouldn't be 1 through 40. Typically you could have
gaps in the sequence, but then you could add code to translate
values above 40 into the gaps.

Hope this helps,

Gabor

suprithaswamy@gmail.com wrote:
> Hi John,
>
> This is not the case i wanted, i want to implement in
> hardware.
> So, i prefer using flops and some of the gates, so i think this lottery
> picker will not help me in this regard.
> I prefer LFSR because it is hardware friendly.
> so can you tell me some solution considering my concerns.
>
>
> regards,
> Supritha