11

I need to generate pseudo-random numbers for my genetic algorithm on a Spartan-3E FPGA and i want to implement it in verilog: could you give me any pointers on this?

akosch
  • 4,326
  • 7
  • 59
  • 80

6 Answers6

16

Of course the random generator by Adam is not synthesizable! You have to explicitly create an LFSR.

Following example might help. It is an 8-bit maximal LFSR

module lfsr(input clk, reset, en, output reg [7:0] q);
  always @(posedge clk or posedge reset) begin
    if (reset)
      q <= 8'd1; // can be anything except zero
    else if (en)
      q <= {q[6:0], q[7] ^ q[5] ^ q[4] ^ q[3]}; // polynomial for maximal LFSR
  end
endmodule;
Roman Starkov
  • 59,298
  • 38
  • 251
  • 324
Aamir
  • 2,943
  • 5
  • 26
  • 28
9

You've already got some good answers, but I'll just point out the canonical guide to LFSRs in FPGAs is here:

http://www.xilinx.com/support/documentation/application_notes/xapp052.pdf

It's a bit Xilinx specific in places (which is OK for your FPGA :) but the principles are transferable to others.

Martin Thompson
  • 16,395
  • 1
  • 38
  • 56
3

There is an online tool that can generate Verilog or VHDL code for a pseudo-random number generator. It's on OutputLogic.com

OutputLogic
  • 756
  • 4
  • 12
3

Typically you'd use the IEEE.math_real uniform function

use IEEE.math_real.all;
procedure UNIFORM (variable Seed1,Seed2:inout integer; variable X:out real);

But do a tiny bit a research on pseudo random number generators (PRNGs) and you'll find many variants that are simple LFSR's - which look remarkably similar to CRC generators.

Here are several resources if you want to roll your own starting from existing, working PRNGs:

http://www.opencores.org/?do=project&who=systemc_rng

http://verificationguild.com/modules.php?name=Downloads&d_op=viewdownload&cid=3

Here's a CRC VHDL code generator:

http://www.easics.be/webtools/crctool

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
Adam Davis
  • 91,931
  • 60
  • 264
  • 330
  • 1
    The OP wanted verilog - but your suggestion to look at LFSRs is sound. Just for my own interest, is that UNIFORM procedure synthesizable? What does it synthesize to? – Marty Apr 16 '09 at 22:13
2

The pointer above to OpenCores has a file in the verilog folder called: rng.v

I have used it in a Spartan-3AN and it works great. My code used the random number generator to select a random PWM after I programmed the part and it covered all the selectable PWMs.

hanleyp
  • 811
  • 9
  • 5
0

I agree with the LFSR. I have made one before and it is used for encryption.

yan bellavance
  • 4,710
  • 20
  • 62
  • 93
  • 4
    I think it's important to note here that LFSRs are useful for pseudo-random number generation, but they are inappropriate for use in real encryption schemes. – wjl Jun 05 '11 at 15:36