1

I have an array of 10 elements. How do I output these in random order without repeating.

4
  • Please add some more detail to the question. Is this testbench code? Does it need to synthesize? What are the "elements"? Commented Sep 27, 2013 at 11:11
  • The technique is simple: use a pseudo random number generator to address your array, store if one has already been used, in which case you take the next. The implementation is highly dependent on whether it should be synthesizable or not... Commented Sep 27, 2013 at 11:47
  • @zennehoy this has to be a synthesize-able . Elements are integers in my case. Commented Sep 30, 2013 at 4:29
  • @BennyBarns It has to be a synthesizeable code. Commented Sep 30, 2013 at 4:30

4 Answers 4

2

For a testbench, use the OSVVM random library to generate random indexes into your array. Or you could shuffle it (using the Fischer-Yates Algorithm in concert with the random library).

If it's something you need to synthesize, then put the array into a RAM block, and generate random addresses (for example using a linear feedback shift register).

Be aware that none of these is properly random, only pseudo-random. If you're attempting anything remotely cryptographic, they are unlikely to be what your want.

Sign up to request clarification or add additional context in comments.

Comments

2

For testbenches, OSVVM's RandomPkg makes this easy.

library osvvm ; 
use osvvm.RandomPkg.all ;
...
RandProc : process
  variable RV : RandomPtype ;
  variable IndexVals : integer_vector(0 to 9) := (others => integer'low) ;
begin
  for i in IndexVals'range loop 
    --                         min  max   ExcludeList
    IndexVals(i) := RV.RandInt(  0,   9,   IndexVals) ;
  end loop ; 

The problem gets more interesting if successive randomly generated permutations of 10 elements need to be different from the previous one. For that I would use the coverage model. Although, 10 is around the maximum number of permutations I would want to do this way though as there are n! permutations and the coverage model will require storage for each permutation generated.

Comments

-1

A good way to get close to randomness is by using a linear feedback shift register.

http://en.wikipedia.org/wiki/Linear_feedback_shift_register

Comments

-1

If this is for simulation only, you can use the uniform procedure from ieee.math_real to get a real number between 0 and 1, and scale it to the index range of your array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.