# How to generate random number between 44 to 55 ?

Status
Not open for further replies.

#### venkatec

##### Newbie level 6
how to generate random numbers between 1 and 55

Hi Dear Frnds,

How to generate random number between 44 to 55 in verilog or VHDL?

#### saurabhmimani

##### Junior Member level 1
random number between 55 to 77 in verilog

using LFSR(Linear feedback Shift Register) u can generate pseudo random numbers out of them use 3 bits to 44 so that u will random numbers from 44 to 52, now add two different bits from LFSR register to this u will get random numbers between 44 and 55.

#### FvM

##### Super Moderator
Staff member
uniform distribution in verilog

Unfortunately, the distribution isn't uniform then.

#### saurabhmimani

##### Junior Member level 1
uniform distribution verilog command

I think if you use a LFSR that have a very long period should give uniform distribution but as you are saying than it may not. However than what other way is to get uniform distribution?

#### FvM

##### Super Moderator
Staff member
By adding two random numbers, you already change the distribution. You can use a mapping of the 1...2^n-1 LFSR number range to 44..55 by a table or a mod 12 operation. In case of a sufficient LFSR bitwidth, the distribution unsymmetry is only small. Dropping the unwanted numbers is another option.

#### saurabhmimani

##### Junior Member level 1
Sorry as I am unable to get how you are generating random numbers, can you plz more elaborate it...

#### FvM

##### Super Moderator
Staff member
LFSR random number generators have an uniform distribution over their respective number range of 1...2^n-1, where n ist the number of stages.

To get an uniform distribution over the range 44..55 respectvively 0..11, you need a mapping that keeps the uiniformity.

I was discussing different mapping options: e.g. look up table or mod 12 operation. Your original suggestion creates also a mapping, but it violates uniformity and has a range of 0..10 instead of required 0..11.

Thanks.

#### venkatec

##### Newbie level 6
Hi Frnd,

I tryed to use the syntax you have given but its printing different values
which are in not range
code follows here
-----------
module test();
reg a;
reg b;
reg c;
wire out;
reg[7] range;

always
repeat(24)
begin
#100
a = 1'b1;
b = 1'b1;

#100
a = 1'b0;
b = 1'b1;

end

always@(a or b)
begin
repeat(24)
begin
range = 55 + $random % 16; end$monitor(" ‰d ",range);
end

endmodule

output follows here

?d 66
# ?d 47
# ?d 58
# ?d 54
# ?d 67
# ?d 67
# ?d 48
# ?d 46
# ?d 57
# ?d 59
# ?d 64
# ?d 49
# ?d 63
# ?d 50
# ?d 69
# ?d 69
# ?d 42
# ?d 41
# ?d 62
# ?d 55
# ?d 55
# ?d 44
# ?d 51
# ?d 47
# ?d 41
# ?d 44
# ?d 42

Could you give me correct solution?

Thanks,
venkat k

Added after 1 hours 6 minutes:

Hi Frnd,

I got the solution for this

we have to use the random command like this.
we should use { } brackets like below.

range = 55 + {\$random} % 16;

This gives values between 55 to 77

Thanks,
venkat k

The above one gives value between 55 to 71
not 55 to 77...

Status
Not open for further replies.