Go Back   Velocity Reviews > Newsgroups > VHDL
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

VHDL - random number generator function

 
Thread Tools Search this Thread
Old 11-22-2007, 05:34 AM   #1
Default random number generator function


Hi,

I am creating a finite state machine, and within one of the states I
need to obtrain a random number by calling a function.

random_value <= rand_val;

I am new to VHDL.

Thanks,



bb
  Reply With Quote
Old 11-22-2007, 06:20 AM   #2
Thomas Stanka
 
Posts: n/a
Default Re: random number generator function
On 22 Nov., 06:34, bb <bineab...@gmail.com> wrote:
> I am creating a finite state machine, and within one of the states I
> need to obtrain a random number by calling a function.
>
> random_value <= rand_val;


Impossible. There is no true random functionality obtainable in HW.
You need something like LFSR (Linear Feedback Shift Register), google
for them and see if they fit your need.
If not, learn more about the problem and come back with more detailed
questions.

bye Thomas



Thomas Stanka
  Reply With Quote
Old 11-22-2007, 11:42 AM   #3
john.williamson@aculab.com
 
Posts: n/a
Default Re: random number generator function
On Nov 22, 6:20 am, Thomas Stanka <usenet_nospam_va...@stanka-web.de>
wrote:
> On 22 Nov., 06:34, bb <bineab...@gmail.com> wrote:
>
> > I am creating a finite state machine, and within one of the states I
> > need to obtrain a random number by calling a function.

>
> > random_value <= rand_val;

>
> Impossible. There is no true random functionality obtainable in HW.
> You need something like LFSR (Linear Feedback Shift Register), google
> for them and see if they fit your need.
> If not, learn more about the problem and come back with more detailed
> questions.
>
> bye Thomas




john.williamson@aculab.com
  Reply With Quote
Old 11-22-2007, 11:47 AM   #4
john.williamson@aculab.com
 
Posts: n/a
Default Re: random number generator function
You could use the 'Uniform' procedure (in the IEEE MATH_REAL package),
but strictly for simulation and modelling puroses only.

As Thomas suggests, a hardware implementation is a completely
different proposition.

John


john.williamson@aculab.com
  Reply With Quote
Old 11-22-2007, 01:58 PM   #5
Mike Treseler
 
Posts: n/a
Default Re: random number generator function
bb wrote:
> I am creating a finite state machine, and within one of the states I
> need to obtrain a random number by calling a function.


I use an lfsr function to make byte sequences
for simulations and packet fcs fields.
It's not random, but might be what you need.
See the testbench here http://home.comcast.net/~mike_treseler/
for details.

-- Mike Treseler
--------------------------------------------------------------------
function randomize (arg_byte : char_t)
return char_t is
variable result_v : char_t;
begin
result_v := arg_byte; -- here it is
result_v := shift_left(result_v, 1); -- shift it
if (result_v(result_v'left)) = '1' then -- maybe invert mask
result_v := result_v xor mask_c; -- bits
end if;
return result_v;
end function randomize;
--------------------------------------------------------------------


Mike Treseler
  Reply With Quote
Old 11-22-2007, 04:58 PM   #6
bb
 
Posts: n/a
Default Re: random number generator function
Psuedo-random is fine. So an LFSR will work.

Mike,

How would I call that function randomize? Thanks.

On Nov 22, 8:58 am, Mike Treseler <mike_trese...@comcast.net> wrote:
> bb wrote:
> > I am creating a finite state machine, and within one of the states I
> > need to obtrain a random number by calling a function.

>
> I use an lfsr function to make byte sequences
> for simulations and packet fcs fields.
> It's not random, but might be what you need.
> See the testbench herehttp://home.comcast.net/~mike_treseler/
> for details.
>
> -- Mike Treseler
> --------------------------------------------------------------------
> function randomize (arg_byte : char_t)
> return char_t is
> variable result_v : char_t;
> begin
> result_v := arg_byte; -- here it is
> result_v := shift_left(result_v, 1); -- shift it
> if (result_v(result_v'left)) = '1' then -- maybe invert mask
> result_v := result_v xor mask_c; -- bits
> end if;
> return result_v;
> end function randomize;
> --------------------------------------------------------------------




bb
  Reply With Quote
Old 11-22-2007, 06:01 PM   #7
Mike Treseler
 
Posts: n/a
Default Re: random number generator function
bb wrote:

> How would I call that function randomize?


random_byte_s <= randomize(random_byte_s);

See the referenced testbench.
Search for "randomize"

-- Mike Treseler


Mike Treseler
  Reply With Quote
Old 11-24-2007, 10:21 PM   #8
bb
 
Posts: n/a
Default Re: random number generator function
This is a finite state machine for "tester", it will generate 10
questions, each question is a random value, and then, the user using
push buttons will enter the bcd value. the tester will then, indicate
if its correct or false.

So, in state 1, ST1, I need a random value to be generated. How will I
include the function randomize in this code? And how will it be called
in ST1?

Yes, this code was tested.



--------------------------------------------------------------
-- FSM for Tester
--------------------------------------------------------------

library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use work.all;

--------------------------------------------------------------

entity tester is

port(
clk: in std_logic;
rst: in std_logic;
bist1: in std_logic;
bist2: in std_logic;
x_i: in std_logic_vector(7 downto 0);
LED: out std_logic_vector(7 downto 0):= "00000000";
SSG: out std_logic_vector(7 downto 0):= "00000000";
);
end tester;

--------------------------------------------------------------

architecture FSMD of tester is
signal tempclk : STD_LOGIC_VECTOR (27 downto
0):="0000000000000000000000000000";
signal clk_1hz : std_logic:='0';
signal clk_8hz : std_logic:= '0';
--signal clk_80hz : std_logic:='0';
-- define states using variable
type S_Type is (ST0, ST1, ST2, ST3, ST4, ST5, ST6);
variable State: S_Type := ST0 ;
variable Data_X: std_logic_vector(7 downto 0);
variable rand: integer range 0 to 255;
variable disp_1: integer range 0 to 9;
variable disp_10: integer range 0 to 9;
variable disp_100: integer range 0 to 9;
variable q_counter: integer range 0 to 10;
variable s_counter: integer range 0 to 9;
variable timer: integer range 0 to 10;
begin
--- divide system clock to get clock of 1Hz
process (clk)
begin
if Clk='1' and Clk'event then
tempclk <= tempclk + "0000000000000000000000000001";
end if;
end process;
--- get the clock of 1Hz by taking 26th bit of tempclk
clk_1hz <= tempclk(25);
clk_8hz <= tempclk(22);

process(rst, clk_8hz)
begin
if (rst='1') then -- initialization
State := ST0;
elsif (bist1 and bist2) then
--BIST CODE HERE
elsif (clk_8hz'event and clk_8hz='1') then
case State is

when ST0 => -- Initialize
--INITALIZE STATE VARIABLES
--dont really have any states to init atm, but might later!
q_counter := 0;
s_counter := 0;
timer := 0:
State := ST1;

when ST1 => -- Generate Random Number
--if the q_counter is 10 then get a new random, otherwise, were
done with the test
if (q_counter = 10)
State:= ST6;
else
--RANDOM NUMBER GENERATION HERE -- up to 255
-- rand <=

--This gives us the value that must be displayed on EACH
SEGEMENT of the Seven Segemnt Display
disp_1 <= ((rand / 1) mod 10);
disp_10 <= ((rand / 10) mod 10);
disp_100 <= ((rand / 100) mod 10);
--DISPLAY RANDOM NUMBER
State := ST2;
end if;

when ST2 => -- Get Input
Data_X := x_i;
State := ST3;

when ST3 => -- IDLE
State := ST4;
when ST4 => -- IDLE
State := ST5;

when ST5 => -- Compare

if (Data_X=Rand) then
--Correct
--Display "YES" message or "correct" message
--Increment Score counter
s_counter <= s_counter + 1;
--Increment Question Counter
q_counter <= q_counter + 1;
--GOTO ST1
State:= ST1;

--Timer 80 should be about 10 seconds
elsif (timer = 80) then
--Time has expired on this question, so lets increment the
question counter
--and move on to the next
q_counter <=q_counter + 1;
State := ST1;
else
--incorrect
--keep showing original random number
--decrement the TIMER
timer = timer + 1;
end if;

when ST6 => -- End of Program
--We're done, report result for all time
rand <= s_counter;

when others => -- UNHANDLED EXCEPTION
State := ST0;
end case;
end if;

end process;
--update on every 80hz tick and every random number update
process (rand,clk_8hz)
begin
--- Control the Display on the 80hz clock
if (clk_8hz = '1' and clk_8hz'event) then
if (AN0 = '0') then

AN0 <= '1';
--General BCD decoder for the 1's place
case disp_1 is
when '0000' => SSG <= "0000001";
when '0001' => SSG <= "1001111";
when '0010' => SSG <= "0010010";
when '0011' => SSG <= "0000110";
when '0100' => SSG <= "1001100";
when '0101' => SSG <= "0100100";
when '0110' => SSG <= "1100000";
when '0111' => SSG <= "0001111";
when '1000' => SSG <= "0000000";
when '1001' => SSG <= "0000001";
when others => SSG <= "0001100";
end case;

AN1 <= '0';
elsif (AN1 = '0') then
AN1 <= '1';
--General BCD decoder for the 10's place
case disp_10 is
when '0000' => SSG <= "0000001";
when '0001' => SSG <= "1001111";
when '0010' => SSG <= "0010010";
when '0011' => SSG <= "0000110";
when '0100' => SSG <= "1001100";
when '0101' => SSG <= "0100100";
when '0110' => SSG <= "1100000";
when '0111' => SSG <= "0001111";
when '1000' => SSG <= "0000000";
when '1001' => SSG <= "0000001";
when others => SSG <= "0001100";
end case;
AN2 <= '0';
elsif (AN2 = '0') then
AN2 <= '1';
--General BCD decoder for the 100's place
case disp_100 is
when '0000' => SSG <= "0000001";
when '0001' => SSG <= "1001111";
when '0010' => SSG <= "0010010";
when '0011' => SSG <= "0000110";
when '0100' => SSG <= "1001100";
when '0101' => SSG <= "0100100";
when '0110' => SSG <= "1100000";
when '0111' => SSG <= "0001111";
when '1000' => SSG <= "0000000";
when '1001' => SSG <= "0000001";
when others => SSG <= "0001100";
end case;
AN0 <= '0';
end if;
end if;
end process;

end FSMD;

--------------------------------------------------------------

On Nov 22, 1:01 pm, Mike Treseler <mike_trese...@comcast.net> wrote:
> bb wrote:
> > How would I call that function randomize?

>
> random_byte_s <= randomize(random_byte_s);
>
> See the referenced testbench.
> Search for "randomize"
>
> -- Mike Treseler




bb
  Reply With Quote
Old 11-25-2007, 11:42 PM   #9
Mike Treseler
 
Posts: n/a
Default Re: random number generator function
bb wrote:
> This is a finite state machine for "tester", it will generate 10
> questions, each question is a random value, and then, the user using
> push buttons will enter the bcd value. the tester will then, indicate
> if its correct or false.
>
> So, in state 1, ST1, I need a random value to be generated. How will I
> include the function randomize in this code? And how will it be called
> in ST1?


> Yes, this code was tested.


That seems unlikely.

See the syntax errors below to start with.
Variables and functions must be declared between
IS and BEGIN of the *PROCESS*.
Consider using clock enables instead of multiple clocks.
The number generator is the least of your problems.
Get the basic controller running with a constant sequence first
Start with a known-good CASE example and simulate that first.
Add features one at a time, and debug each one.

A newsgroup like this will answer specific questions
or make comments and suggestions
once you have some working code, but it
is unreasonable to expect more.
Consider finding a local tutor.
Good luck.

-- Mike Treseler

__________________________
vcom -2002 -quiet -work work tester.vhd
** Error: tester.vhd(35): near ")": expecting: IDENTIFIER
** Error: tester.vhd(83): near "0:": (vcom-111) No digits found in
mantissa part of based literal.
** Error: tester.vhd(83): near "0:": (vcom-113) Mantissa part of based
integer literal terminates with '
'; should be ':'.
** Error: tester.vhd(83): near "0:": (vcom-11 Base value 0 is not in
range 2 to 16.
** Error: tester.vhd(84): near ":=": expecting: ';'
** Error: tester.vhd(90): near "State": expecting: GENERATE THEN
** Error: tester.vhd(104): near "when": expecting: END
** Error: tester.vhd(10: near "when": expecting: END


Mike Treseler
  Reply With Quote
Old 11-26-2007, 12:26 AM   #10
KJ
 
Posts: n/a
Default Re: random number generator function

"Mike Treseler" <> wrote in message
news:...
> bb wrote:
>
>> Yes, this code was tested.

>
> That seems unlikely.
>

Testing produces results....one result of testing is error(s)....'bb' never
said the testing produced no errors
..
KJ




KJ
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with code for random number generation sandeep_sp7 Hardware 0 04-12-2007 04:59 PM
New releases: World Trade Center, Jackass Number Two & Wicker Man: Updated complete downloadable R1 DVD DB & info lists Doug MacLean DVD Video 0 10-24-2006 06:04 AM
This is incredible! jc_ice DVD Video 1 08-13-2006 10:47 AM
Re: 38,000 U.S. KILLED & WOUNDED IN IRAQ & dvd's JA DVD Video 0 03-06-2005 01:16 AM
Counting In Binary Raymond A+ Certification 13 03-07-2004 07:28 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46