Search code examples
hardwareverilogfpgahdl

How to choose a random number within a given time?


I want that an LED should turn on at any random time within 15 seconds. For example after pressing a button it should turn on after 4 seconds or 7 seconds, that is randomly.

I came up with the code to produce a 15 second delay, but I cant figure out a way to select a random time between this.

Below is my code for the 15 second delay:

always @ (posedge clock or posedge reset)
begin
 if(reset)

  ticker <= 0;

 else if(ticker == 750000000) //if it reaches the desired max value that equates 15 second reset it
  ticker <= 0;
 else if(start) //only start if the input is set high
  ticker <= ticker + 1;
end

assign click = ((ticker == 750000000)?1'b1:1'b0); //click to be assigned high every 0.1 second

Also I want a synthesizable solution please.


Solution

  • You need a hardware way to create a random number. You have $random for simulation but not hardware.

    I would suggest creating an lfsr which runs and when a button is pressed, you capture the current value which you count up to, or preset a counter and count down to 0.