Forum Discussion
Altera_Forum
Honored Contributor
9 years agonot random at each execution
Hi all,
I have a problem with generating a random from the uniform function, it is always the same with each execution of the process. I have attached a piece of code to the testbench. If someone gets to see where it's wrong. Thank you16 Replies
- Altera_Forum
Honored Contributor
Looks fine from the small low res picture, but I cannot see the whole file.
Why do you assign int_rand back to itself? Why not post the code so we can try it - Altera_Forum
Honored Contributor
Thank you for ur reply,
here is my code, it should be enough to test it --- Quote Start --- --main process p_main: process (clk, rst_n) variable seed1, seed2 : positive; variable rand : real; variable int_rand : integer range 0 to 1000; variable counter, count_hash, num_test: integer; variable line_in, line_out : line; variable temp: std_logic_vector(63 downto 0); file filein : text open read_mode is "c:\users\simo\Desktop\Keccak_core\new_test_vector/keccak_in.txt"; file fileout : text open write_mode is "c:\users\simo\Desktop\Keccak_core\new_test_vector/keccak_out_compact_vhdl.txt"; begin if rst_n = '0' then -- asynchronous rst_n (active low) st <= initial; counter:=0; din<=(others=>'0'); count_hash:=0; init<='0'; absorb<='0'; squeeze<='0'; go<='0'; rand_num <= 1; elsif clk'event and clk = '1' then -- rising clk edge uniform (seed1, seed2, rand); int_rand := INTEGER(TRUNC(rand*1000.0)); int_rand := int_rand; report "random is :" & integer'image(int_rand) severity NOTE; --is not random at each execution --- Quote End --- - Altera_Forum
Honored Contributor
I assign int_rand back to itself by mistake, we can even comment it if we want
- Altera_Forum
Honored Contributor
Your code (once I removed all the illegal signal assignments) works fine:
# ** Note: random is :999 # Time: 40 ns Iteration: 0 Instance: /tester # ** Note: random is :974 # Time: 50 ns Iteration: 0 Instance: /tester # ** Note: random is :647 # Time: 60 ns Iteration: 0 Instance: /tester # ** Note: random is :333 # Time: 70 ns Iteration: 0 Instance: /tester # ** Note: random is :36 # Time: 80 ns Iteration: 0 Instance: /tester # ** Note: random is :161 # Time: 90 ns Iteration: 0 Instance: /tester # ** Note: random is :664 # Time: 100 ns Iteration: 0 Instance: /tester # ** Note: random is :84 # Time: 110 ns Iteration: 0 Instance: /tester # ** Note: random is :204 # Time: 120 ns Iteration: 0 Instance: /tester # ** Note: random is :167 # Time: 130 ns Iteration: 0 Instance: /tester # ** Note: random is :654 # Time: 140 ns Iteration: 0 Instance: /tester # ** Note: random is :128 # Time: 150 ns Iteration: 0 Instance: /tester # ** Note: random is :910 # Time: 160 ns Iteration: 0 Instance: /tester # ** Note: random is :111 # Time: 170 ns Iteration: 0 Instance: /tester # ** Note: random is :299 # Time: 180 ns Iteration: 0 Instance: /tester # ** Note: random is :265 # Time: 190 ns Iteration: 0 Instance: /tester # ** Note: random is :699 # Time: 200 ns Iteration: 0 Instance: /tester - Altera_Forum
Honored Contributor
yes you are rghit it gives that, but If you run it another time it will give u the same numbers so it's not random in my opinion !
- Altera_Forum
Honored Contributor
--- Quote Start --- yes you are rghit it gives that, but If you run it another time it will give u the same numbers so it's not random in my opinion ! --- Quote End --- It is perfectly psuedorandom - you provide the seed values to determine the sequence. It is important that you can recreate the sequence in verfication. If you run a test with different seeds, only one sequence my produce a fault. Try changing the seeds, and you'll see the sequence is different. - Altera_Forum
Honored Contributor
--- Quote Start --- It is perfectly psuedorandom - you provide the seed values to determine the sequence. It is important that you can recreate the sequence in verfication. If you run a test with different seeds, only one sequence my produce a fault. Try changing the seeds, and you'll see the sequence is different. --- Quote End --- Thank you for your reactions, that's what I thought. But the seeds are provided by the uniform fucntion, I don't see how to make them change at each exectuion ? - Altera_Forum
Honored Contributor
You don't. You provide an initial value and then they change on each iteration.
- Altera_Forum
Honored Contributor
The rand function in c works in a similar way.
- Altera_Forum
Honored Contributor
yes u are right it's random on each iteration.
To give you an idea of what I'm looking for. I use the result of the rand to add it to the result of a hash function that I want it to be random at each execution. so when i use the int_rand wich the result of uniform iget the same thing