Randomly distribute ones in a n bit vector

You can use either

void'( randomize(vector) with { $countones(vector) == cross_sum } );

or

bit temp_array[n];
bit [n-1:0] vector;
int cross_sum;

  void'(randomize(temp_array) with { (temp_array.sum(b) with (int'(b)) == cross_sum); } );
  vector = type(vector)'(temp_array);