Random Number selection

D

Deepu

Guest
Hi All,

Can somebody help me with a better approach to perform the random
number selection from a set of numbers than the one mentoned below.

If there are set of numbers like & i would like to get one number out
of this:

0,1,2,3,256,257.258,259,512,513,514,515,768,769,770,771

int ii;
int number;

ii = {$random(seed)} % 2;

number = 0 + {$random(seed)} % 4 + (0 + ($urandom_range(259,256)) *
ii) + (0 + ($urandom_range(515,512)) * ii) + (0 +
($urandom_range(771,768)) * ii)


Thanks for your time..
 
On Wed, 26 May 2010 11:32:43 -0700 (PDT), Deepu wrote:

Can somebody help me with a better approach to perform the random
number selection from a set of numbers than the one mentoned below.
Depends how much SystemVerilog you would like to try.

Answer #1 (easy in any Verilog dialect):
Populate an array with your numbers. Randomly pick an index.

int values[16] = '{ 0, 1, 2, 3,
256,257.258,259,
512,513,514,515,
768,769,770,771 };
number = values[$dist_uniform(seed, 0, 15)];

Answer #2 (SystemVerilog testbench licence required):

int number;
std::randomize(number) with { number inside
{[0:3], [256:259], [512:515], [768:771]};};

Answer #3
(easy, but depends on special properties of the set of numbers):
Observe that the numbers are all of the form 256*A+B, where
A and B are both in the range 0..3. Choose random values
for A and B, then construct the number.

An Observation:
~~~~~~~~~~~~~~~
Of the three methods I offered above, just one (#2) depends
on detailed knowledge of (System)Verilog. The other two
are based solely on simple arithmetical or logical insight.
Since it's a fair bet that you are younger and therefore
faster-thinking than I am, I wonder why it needed me to
point them out?
--
Jonathan Bromley
 
Answer #3
(easy, but depends on special properties of the set of numbers):
Observe that the numbers are all of the form 256*A+B, where
A and B are both in the range 0..3.  Choose random values
for A and B, then construct the number.
Thanks! Jonathan 3rd version was more comfortable for me. Thanks for
all the other approach, would have liked to check the 2nd approach but
was not able because of the license..
 

Welcome to EDABoard.com

Sponsor

Back
Top