Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

trying to generate a random number in a particular range using lfsr

Status
Not open for further replies.

vhdl34

Newbie level 6
Newbie level 6
Joined
Nov 13, 2012
Messages
12
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,349
i m using lfsr to generate a number from 145 to 786 howwver the same 3 numbers are appering can you please help me here under is my code
Code:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;



USE ieee.numeric_std.all;
entity random is
    
port (
      clkin : in std_logic;
      random_num : out integer   --output vector            
    );
end random;

architecture Behavioral of random is
begin
process(clkin)
variable rand_temp : std_logic_vector(9 downto 0):=( 9=> '1',others => '0');
variable temp : std_logic := '0';
variable rand: std_logic_vector(9 downto 0);
variable check : integer :=0;
variable state : std_logic := '0';
begin
if(rising_edge(clkin)) then

temp := rand_temp(9) xor rand_temp(8);
rand_temp(8 downto 0) := rand_temp(9 downto 1);
rand_temp(0) := temp;

if (rand_temp < "0010010001" )then
   rand_temp := "0100011000";
end if;
if (rand_temp > "1100001010" )then
   rand_temp := "0110010001";
end if;

check := to_integer(unsigned(rand_temp));



random_num <= check;
end if;
end process;
end Behavioral;
 

You have not infere the LSFR generator. Let it generate 1023 data in its period.
To put the data into the given range you can:
- select from the data flow the output data which satisfy limitations, or
-add 145 and scale the result by multiplication to (786-145)/1023, or
- combine first two approaches .
 

To clarify, the upper two bits as the feedback taps only works for a few lengths. Simply try a 3b lfsr, 4b lfsr, and 5b lfsr. The 3b/4b will work, but the 5b lfsr will have cycles of different length based on the initial value of the state register. If you want to use two taps, try the 15b lfsr as the two msb's are correct for a sequence of length 2**15 - 1. You can then select a subset of the bits. (10b can use bits 9,6 for a maximal length sequence. see https://en.wikipedia.org/wiki/Linear_feedback_shift_register#Some_polynomials_for_maximal_LFSRs )

A second issue is the line "rand_temp(8 downto 0) := rand_temp(9 downto 1);", which is backwards -- bit 9 never gets set in the lfsr operation because the bits shift right.

there are other methods as well, eg lookup tables, lfsr's that reset on specific values, counters, etc... You haven't said what you consider to be random. The range check will give you more samples at 768 and 145 than a random sequence, but this may be ok for your application.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top