Skip to main content
2 of 4
added 255 characters in body
kglr
  • 400.5k
  • 18
  • 488
  • 929

A combination of IntegerPartitions, RandomChoiceand RandomSample:

n = 30;
RandomSample[#, n] & @ RandomChoice @ IntegerPartitions[0, {n}, {-1, 0, 1}]

{-1, 1, 1, 1, 1, -1, -1, 0, -1, 1, 1, -1, -1, -1, 1, -1, 1, 1, -1, 0, 1, -1, -1, 1, -1, -1, 1, 1, -1, 1}

Total @ %

0

You can also do

RandomSample[#, n]& @
 PadRight[Flatten @ ConstantArray[{1, -1}, RandomChoice[Range[Floor[n/2]]]], n]

{-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, -1, -1, -1, 0, 0, -1, 0, 0, 0, 0, 1, 0}

Total @ %

0

kglr
  • 400.5k
  • 18
  • 488
  • 929