Skip to main content
deleted 15 characters in body
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929

A combination of IntegerPartitions, RandomChoiceand RandomSample:

n = 30;
RandomSample[#, n] &RandomSample @ 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]&RandomSample @
 PadRight[Flatten @ ConstantArray[{1, -1}, RandomChoice[Range[0, 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

For large n, the second approach is much faster than the first.

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[0, 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

For large n, the second approach is much faster than the first.

A combination of IntegerPartitions, RandomChoiceand RandomSample:

n = 30;
RandomSample @ 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 @
 PadRight[Flatten @ ConstantArray[{1, -1}, RandomChoice[Range[0, 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

For large n, the second approach is much faster than the first.

added 67 characters in body
Source Link
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[nRandomChoice[Range[0, 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

For large n, the second approach is much faster than the first.

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

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[0, 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

For large n, the second approach is much faster than the first.

added 255 characters in body
Source Link
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

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

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

Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929
Loading