Deepu
Guest
Tue May 25, 2010 2:30 am
Hi,
I have a function as below and would like to get some help on making
sure how i get unique numbers everytime this function is called & if
the 'number' passed to this function is always '0'.
function int fn_get_number (
input int number = 0
);
int temp_number;
case (number)
16'd0 : temp_number = 0 + {$random(seed)} % 11;
16'd1 : temp_number = 12 + {$random(seed)} % 11;
16'd84 : temp_number = 100 + {$random(seed)} % 11;
16'd999 : sector_number = {$random(seed)} % 1000;
endcase
fn_get_number = temp_number;
endfunction
Thanks for your time.
Jonathan Bromley
Guest
Tue May 25, 2010 10:20 am
On May 25, 1:00 am, Deepu wrote:
Quote:
I have a function as below and would like to get some help on making
sure how i get unique numbers everytime this function is called & if
the 'number' passed to this function is always '0'.
function int fn_get_number (
input int number = 0
);
int temp_number;
case (number)
16'd0 : temp_number = 0 + {$random(seed)} % 11;
16'd1 : temp_number = 12 + {$random(seed)} % 11;
16'd84 : temp_number = 100 + {$random(seed)} % 11;
16'd999 : sector_number = {$random(seed)} % 1000;
endcase
fn_get_number = temp_number;
endfunction
Deepu, what on earth do you mean by "unique" here? If the
function is called with number=0, there are only 11 possible
values it can return (0 thru 10). If you call the function
12 times, at least one of those numbers MUST be duplicated!
One common requirement is to generate the numbers (0 thru 10
in this case) in some random sequence, and then generate
the same set of numbers in a different random sequence -
in fact, a "shuffle", as if the numbers were a deck of
cards and you were drawing from the deck one by one. This
can be implemented in various ways, but since you're
evidently using SystemVerilog the simplest is probably
to use "randc" (cyclic randomize). This example should
give you enough hints to get started.
module test_randc;
class C;
randc bit [3:0] r;
constraint mod_11 {r < 11;}
endclass
int ok;
C c = new;
initial repeat (20) begin
ok = c.randomize();
$display("rand(%0d): c.r=%0d", ok, c.r);
end
endmodule
Hope this helps
--
Jonathan Bromley
Deepu
Guest
Tue May 25, 2010 5:11 pm
Quote:
Deepu, what on earth do you mean by "unique" here? If the
function is called with number=0, there are only 11 possible
values it can return (0 thru 10). If you call the function
12 times, at least one of those numbers MUST be duplicated!
Hi Jonathan,
Actually even if i call the function 5 times, it gave me same number
twice. So that is the reason i was trying to make sure if i call the
function 5 times i get 5 unique numbers.
Thanks
Jonathan Bromley
Guest
Tue May 25, 2010 7:00 pm
Deepu,
Quote:
Actually even if i call the function 5 times, it gave me same number
twice. So that is the reason i was trying to make sure if i call the
function 5 times i get 5 unique numbers.
Well, if you *really* want a permutation that's fine, but you
do need to recognise that a random number stream will usually include
some duplicates before it has exhausted all the possibilities.
Otherwise it wouldn't be very random (think about your 11 numbers:
suppose you draw 10 different numbers in the first 10 tries, and
you insist that you have no duplicates over the first 11 tries:
you then know exactly what the 11th number is going to be!)
If you don't want to use "randc" then there are other ways to do it.
Build an array containing the set of numbers...
int deck[11];
int count = 11;
function void reset_deck();
foreach (deck[i]) deck[i] = i;
endfunction
Now, when you pick a number at random, make sure you remove
that number from the available set for the next pick:
function int pick();
int choice;
// pick one from the available set
index = $dist_uniform(seed, 0, count-1);
choice = deck[index];
// move the last member of the set into that slot,
// to eliminate the one you just picked
deck[index] = deck[count-1];
// and reduce the size of the set
count--;
// and rebuild the set if you've exhausted it
if (count == 0) reset_deck();
// and pass back the result
return choice;
endfunction
cheers
--
Jonathan Bromley