RandomInteger with equal number of 1 and -1Random numbers that sum up to specific valueCompare C++ Standard Library's Mersenne Twister with Mathematica's Mersenne TwisterRandom numbers that sum up to specific valueGenerate non-overlapping permutationsHow to generate two group of $n$ random numbers in $U(0,1)$ such that sum of these two groups equal?Plotting randomly generated pointsAdding random numbers to the arguments of a sum of cosinesProblems with RandomChoiceGenerate a random Fibonacci sequenceUniformly distributed numbers fulfilling conditionsGenerating random coordinates (3D) with constraint

What is the highest voltage from the power supply a Raspberry Pi 3 B can handle without getting damaged?

Is there any proof that high saturation and contrast makes a picture more appealing in social media?

UK - Working without a contract. I resign and guy wants to sue me

Why is it easier to balance a non-moving bike standing up than sitting down?

Trainee keeps passing deadlines for independent learning

How can you guarantee that you won't change/quit job after just couple of months?

Excluding a rectangular region from an image in FITS

Where's this swanky house and vineyard near a mountain?

Boss wants someone else to lead a project based on the idea I presented to him

What are Elsa's reasons for selecting the Holy Grail on behalf of Donovan?

Constitutionality of U.S. Democratic Presidential Candidate's Supreme Court Suggestion

LWC - Local Dev - How can I run the local server on HTTPS?

Android Material and appcompat Manifest merger failed in react-native or ExpoKit

Encounter design and XP thresholds

How many people are necessary to maintain modern civilisation?

How to execute a command when ALL of the players are close enough

Why does independence imply zero correlation?

Methodology: Writing unit tests for another developer

Should I include an appendix for inessential, yet related worldbuilding to my story?

Prime sieve in Python

What is "industrial ethernet"?

Confusion over 220 and 230 volt outlets

Helping ease my back pain by studying 13 hours everyday , even weekends

Greeting with "Ho"



RandomInteger with equal number of 1 and -1


Random numbers that sum up to specific valueCompare C++ Standard Library's Mersenne Twister with Mathematica's Mersenne TwisterRandom numbers that sum up to specific valueGenerate non-overlapping permutationsHow to generate two group of $n$ random numbers in $U(0,1)$ such that sum of these two groups equal?Plotting randomly generated pointsAdding random numbers to the arguments of a sum of cosinesProblems with RandomChoiceGenerate a random Fibonacci sequenceUniformly distributed numbers fulfilling conditionsGenerating random coordinates (3D) with constraint






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








3












$begingroup$


I want to generate a list of random integers with only three values 1,0,-1. I know it can be done through RandomInteger[-1, 1, n]. How can I impose constraint on this list so that every integers in this list add up to 0?



(I have tried some approaches in Random numbers that sum up to specific value, but they did not work)










share|improve this question









$endgroup$











  • $begingroup$
    Does not the condition that the integers sum to 0 violate their being "random"?
    $endgroup$
    – murray
    8 hours ago










  • $begingroup$
    @murray They're not independent, but the whole list is still random I think
    $endgroup$
    – Chris K
    8 hours ago










  • $begingroup$
    You can certainly have a "restricted randomization" such that the sum of the numbers equals zero. But you'll need to add more structure to your request. For example, you could have the probability of selecting -1 and 1 be zero and the probability of 0 being 1. You'd end up with all zeros and satisfy both the randomness and the restriction. You could have the probabilities of -1, 0, and 1 being $p$, $1-2p$, and $p$, respectively, with the added restriction that the sum equal zero.
    $endgroup$
    – JimB
    7 hours ago










  • $begingroup$
    Are you interested in uniform sampling of the possible tuples?
    $endgroup$
    – Carl Woll
    5 hours ago

















3












$begingroup$


I want to generate a list of random integers with only three values 1,0,-1. I know it can be done through RandomInteger[-1, 1, n]. How can I impose constraint on this list so that every integers in this list add up to 0?



(I have tried some approaches in Random numbers that sum up to specific value, but they did not work)










share|improve this question









$endgroup$











  • $begingroup$
    Does not the condition that the integers sum to 0 violate their being "random"?
    $endgroup$
    – murray
    8 hours ago










  • $begingroup$
    @murray They're not independent, but the whole list is still random I think
    $endgroup$
    – Chris K
    8 hours ago










  • $begingroup$
    You can certainly have a "restricted randomization" such that the sum of the numbers equals zero. But you'll need to add more structure to your request. For example, you could have the probability of selecting -1 and 1 be zero and the probability of 0 being 1. You'd end up with all zeros and satisfy both the randomness and the restriction. You could have the probabilities of -1, 0, and 1 being $p$, $1-2p$, and $p$, respectively, with the added restriction that the sum equal zero.
    $endgroup$
    – JimB
    7 hours ago










  • $begingroup$
    Are you interested in uniform sampling of the possible tuples?
    $endgroup$
    – Carl Woll
    5 hours ago













3












3








3





$begingroup$


I want to generate a list of random integers with only three values 1,0,-1. I know it can be done through RandomInteger[-1, 1, n]. How can I impose constraint on this list so that every integers in this list add up to 0?



(I have tried some approaches in Random numbers that sum up to specific value, but they did not work)










share|improve this question









$endgroup$




I want to generate a list of random integers with only three values 1,0,-1. I know it can be done through RandomInteger[-1, 1, n]. How can I impose constraint on this list so that every integers in this list add up to 0?



(I have tried some approaches in Random numbers that sum up to specific value, but they did not work)







random constraint integer-sequence






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 8 hours ago









lollol

1096




1096











  • $begingroup$
    Does not the condition that the integers sum to 0 violate their being "random"?
    $endgroup$
    – murray
    8 hours ago










  • $begingroup$
    @murray They're not independent, but the whole list is still random I think
    $endgroup$
    – Chris K
    8 hours ago










  • $begingroup$
    You can certainly have a "restricted randomization" such that the sum of the numbers equals zero. But you'll need to add more structure to your request. For example, you could have the probability of selecting -1 and 1 be zero and the probability of 0 being 1. You'd end up with all zeros and satisfy both the randomness and the restriction. You could have the probabilities of -1, 0, and 1 being $p$, $1-2p$, and $p$, respectively, with the added restriction that the sum equal zero.
    $endgroup$
    – JimB
    7 hours ago










  • $begingroup$
    Are you interested in uniform sampling of the possible tuples?
    $endgroup$
    – Carl Woll
    5 hours ago
















  • $begingroup$
    Does not the condition that the integers sum to 0 violate their being "random"?
    $endgroup$
    – murray
    8 hours ago










  • $begingroup$
    @murray They're not independent, but the whole list is still random I think
    $endgroup$
    – Chris K
    8 hours ago










  • $begingroup$
    You can certainly have a "restricted randomization" such that the sum of the numbers equals zero. But you'll need to add more structure to your request. For example, you could have the probability of selecting -1 and 1 be zero and the probability of 0 being 1. You'd end up with all zeros and satisfy both the randomness and the restriction. You could have the probabilities of -1, 0, and 1 being $p$, $1-2p$, and $p$, respectively, with the added restriction that the sum equal zero.
    $endgroup$
    – JimB
    7 hours ago










  • $begingroup$
    Are you interested in uniform sampling of the possible tuples?
    $endgroup$
    – Carl Woll
    5 hours ago















$begingroup$
Does not the condition that the integers sum to 0 violate their being "random"?
$endgroup$
– murray
8 hours ago




$begingroup$
Does not the condition that the integers sum to 0 violate their being "random"?
$endgroup$
– murray
8 hours ago












$begingroup$
@murray They're not independent, but the whole list is still random I think
$endgroup$
– Chris K
8 hours ago




$begingroup$
@murray They're not independent, but the whole list is still random I think
$endgroup$
– Chris K
8 hours ago












$begingroup$
You can certainly have a "restricted randomization" such that the sum of the numbers equals zero. But you'll need to add more structure to your request. For example, you could have the probability of selecting -1 and 1 be zero and the probability of 0 being 1. You'd end up with all zeros and satisfy both the randomness and the restriction. You could have the probabilities of -1, 0, and 1 being $p$, $1-2p$, and $p$, respectively, with the added restriction that the sum equal zero.
$endgroup$
– JimB
7 hours ago




$begingroup$
You can certainly have a "restricted randomization" such that the sum of the numbers equals zero. But you'll need to add more structure to your request. For example, you could have the probability of selecting -1 and 1 be zero and the probability of 0 being 1. You'd end up with all zeros and satisfy both the randomness and the restriction. You could have the probabilities of -1, 0, and 1 being $p$, $1-2p$, and $p$, respectively, with the added restriction that the sum equal zero.
$endgroup$
– JimB
7 hours ago












$begingroup$
Are you interested in uniform sampling of the possible tuples?
$endgroup$
– Carl Woll
5 hours ago




$begingroup$
Are you interested in uniform sampling of the possible tuples?
$endgroup$
– Carl Woll
5 hours ago










4 Answers
4






active

oldest

votes


















8












$begingroup$

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.






share|improve this answer











$endgroup$












  • $begingroup$
    RandomSample with no second argument gives a random permutation: RandomSample@RandomChoice@IntegerPartitions[0, n, -1, 0, 1]
    $endgroup$
    – Roman
    7 hours ago











  • $begingroup$
    @Roman, thank you; very good point.
    $endgroup$
    – kglr
    7 hours ago


















4












$begingroup$

You need more specificity. @kglr and @Alan give two possibilities (equal probabilities of partitions that sum to zero and (generally) equal numbers of -1's, 0's, and +1's, respectively).



If you want to consider a model where for any particular sample comes from a multinomial distribution with probabilities $p_-1$, $p_0$, and $p_1$ (subject to $p_-1+p_0+p_1=1$ and $0<p_-1<1$ and $0<p_0<1$ and $0<p_1<1$) and under the restriction that the sum of $n$ samples is zero can be found in the following manner:



(* Suppose we have the following: *)
n = 100;
p0 = 1/2;

(* Generate a number of zeros such that n - n0 is even and n0 is not negative *)
SeedRandom[12345];
n0 = -1;
While[n0 < 0 || OddQ[n - n0], n0 = RandomVariate[BinomialDistribution[n, p0]]];

(* The number of -1's and 1's much be each equal to (n-n0)/2 *)
x = If[n0 == 0, RandomSample[Join[ConstantArray[-1, n/2], ConstantArray[1, n/2]]],
If[n0 == n, ConstantArray[0, n],
RandomSample[Join[ConstantArray[-1, (n - n0)/2], ConstantArray[0, nZero],
ConstantArray[1, (n - n0)/2]]]]];
x
(* 0,1,0,-1,1,0,0,-1,1,0,1,-1,1,0,0,-1,0,0,1,0,1,-1,0,-1,0,-1,0,0,0,
1,1,-1,0,0,1,0,0,-1,1,0,1,1,1,-1,-1,0,1,1,0,1,1,1,0,0,-1,1,0,0,0,0,-1,
-1,-1,1,0,-1,0,0,0,1,1,-1,0,-1,1,1,-1,0,-1,0,0,0,0,-1,0,-1,0,0,0,0,-1,
0,-1,-1 *)

Total[x]
(* 0 *)


Notice that because the sum is required to be zero, any values of $p_-1$ and $p_1$ result in the same restricted random distribution once $p_0$ is set.






share|improve this answer









$endgroup$




















    2












    $begingroup$

    If you want to sample uniformly from all possible tuples that sum to 0, you can do the following:



    zero[n_] := With[

    ones = RandomChoice[
    Table[Multinomial[i,i,n-2i], i,0,Floor[n/2]] -> Range[0,Floor[n/2]]
    ]
    ,

    RandomSample @ PadRight[
    Join[ConstantArray[1,ones],ConstantArray[-1,ones]],
    n
    ]
    ]


    For example, here's a tally of the random 4-tuples summing to 0:



    SeedRandom[1];
    Tally @ Table[zero[4], 10^5]



    1, 1, -1, -1, 5215, -1, 1, 0, 0, 5353, -1, -1, 1, 1,
    5381, 1, -1, 0, 0, 5167, 1, -1, -1, 1, 5169, 0, 1, 0, -1,
    5189, 0, -1, 1, 0, 5311, -1, 1, -1, 1, 5263, 0, -1, 0, 1,
    5376, 0, 0, 1, -1, 5268, 1, 0, -1, 0, 5303, 0, 0, 0, 0,
    5218, 1, 0, 0, -1, 5220, 1, -1, 1, -1, 5095, 0, 0, -1, 1,
    5245, -1, 0, 0, 1, 5313, -1, 1, 1, -1, 5253, 0, 1, -1, 0,
    5264, -1, 0, 1, 0, 5397




    Looks pretty close to uniform sampling.



    As a comparison, note the distribution using the accepted answer:



    nonuniform[n_] := RandomSample @ PadRight[
    Flatten@ConstantArray[1,-1,RandomChoice[Range[0,Floor[n/2]]]],
    n
    ]

    Tally @ Table[nonuniform[4], 10^5]



    -1, 0, 1, 0, 2739, 0, 0, 0, 0, 33695, 0, 1, 0, -1,
    2771, -1, -1, 1, 1, 5682, 0, 0, 1, -1, 2807, 1, -1, 0, 0,
    2697, 0, 0, -1, 1, 2790, -1, 0, 0, 1, 2765, -1, 1, 0, 0,
    2738, 0, -1, 0, 1, 2654, -1, 1, -1, 1, 5482, 1, -1, -1, 1,
    5555, 0, -1, 1, 0, 2768, 1, 0, -1, 0, 2721, -1, 1, 1, -1,
    5519, 1, 1, -1, -1, 5512, 0, 1, -1, 0, 2727, 1, 0, 0, -1,
    2710, 1, -1, 1, -1, 5668




    Not very uniform.






    share|improve this answer











    $endgroup$




















      1












      $begingroup$

      Given the lack of details in your specification, I suspect the following very simple approach will be adequate to your needs:



      zeroSum[n_] := With[
      n3 = Floor[n/3]
      ,
      RandomSample@Catenate@
      ConstantArray[-1, n3],
      ConstantArray[1, n3],
      ConstantArray[0, n - 2*n3]
      ]





      share|improve this answer









      $endgroup$













        Your Answer








        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "387"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: false,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        imageUploader:
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        ,
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













        draft saved

        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f200597%2frandominteger-with-equal-number-of-1-and-1%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        8












        $begingroup$

        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.






        share|improve this answer











        $endgroup$












        • $begingroup$
          RandomSample with no second argument gives a random permutation: RandomSample@RandomChoice@IntegerPartitions[0, n, -1, 0, 1]
          $endgroup$
          – Roman
          7 hours ago











        • $begingroup$
          @Roman, thank you; very good point.
          $endgroup$
          – kglr
          7 hours ago















        8












        $begingroup$

        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.






        share|improve this answer











        $endgroup$












        • $begingroup$
          RandomSample with no second argument gives a random permutation: RandomSample@RandomChoice@IntegerPartitions[0, n, -1, 0, 1]
          $endgroup$
          – Roman
          7 hours ago











        • $begingroup$
          @Roman, thank you; very good point.
          $endgroup$
          – kglr
          7 hours ago













        8












        8








        8





        $begingroup$

        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.






        share|improve this answer











        $endgroup$



        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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 7 hours ago

























        answered 8 hours ago









        kglrkglr

        199k10227453




        199k10227453











        • $begingroup$
          RandomSample with no second argument gives a random permutation: RandomSample@RandomChoice@IntegerPartitions[0, n, -1, 0, 1]
          $endgroup$
          – Roman
          7 hours ago











        • $begingroup$
          @Roman, thank you; very good point.
          $endgroup$
          – kglr
          7 hours ago
















        • $begingroup$
          RandomSample with no second argument gives a random permutation: RandomSample@RandomChoice@IntegerPartitions[0, n, -1, 0, 1]
          $endgroup$
          – Roman
          7 hours ago











        • $begingroup$
          @Roman, thank you; very good point.
          $endgroup$
          – kglr
          7 hours ago















        $begingroup$
        RandomSample with no second argument gives a random permutation: RandomSample@RandomChoice@IntegerPartitions[0, n, -1, 0, 1]
        $endgroup$
        – Roman
        7 hours ago





        $begingroup$
        RandomSample with no second argument gives a random permutation: RandomSample@RandomChoice@IntegerPartitions[0, n, -1, 0, 1]
        $endgroup$
        – Roman
        7 hours ago













        $begingroup$
        @Roman, thank you; very good point.
        $endgroup$
        – kglr
        7 hours ago




        $begingroup$
        @Roman, thank you; very good point.
        $endgroup$
        – kglr
        7 hours ago













        4












        $begingroup$

        You need more specificity. @kglr and @Alan give two possibilities (equal probabilities of partitions that sum to zero and (generally) equal numbers of -1's, 0's, and +1's, respectively).



        If you want to consider a model where for any particular sample comes from a multinomial distribution with probabilities $p_-1$, $p_0$, and $p_1$ (subject to $p_-1+p_0+p_1=1$ and $0<p_-1<1$ and $0<p_0<1$ and $0<p_1<1$) and under the restriction that the sum of $n$ samples is zero can be found in the following manner:



        (* Suppose we have the following: *)
        n = 100;
        p0 = 1/2;

        (* Generate a number of zeros such that n - n0 is even and n0 is not negative *)
        SeedRandom[12345];
        n0 = -1;
        While[n0 < 0 || OddQ[n - n0], n0 = RandomVariate[BinomialDistribution[n, p0]]];

        (* The number of -1's and 1's much be each equal to (n-n0)/2 *)
        x = If[n0 == 0, RandomSample[Join[ConstantArray[-1, n/2], ConstantArray[1, n/2]]],
        If[n0 == n, ConstantArray[0, n],
        RandomSample[Join[ConstantArray[-1, (n - n0)/2], ConstantArray[0, nZero],
        ConstantArray[1, (n - n0)/2]]]]];
        x
        (* 0,1,0,-1,1,0,0,-1,1,0,1,-1,1,0,0,-1,0,0,1,0,1,-1,0,-1,0,-1,0,0,0,
        1,1,-1,0,0,1,0,0,-1,1,0,1,1,1,-1,-1,0,1,1,0,1,1,1,0,0,-1,1,0,0,0,0,-1,
        -1,-1,1,0,-1,0,0,0,1,1,-1,0,-1,1,1,-1,0,-1,0,0,0,0,-1,0,-1,0,0,0,0,-1,
        0,-1,-1 *)

        Total[x]
        (* 0 *)


        Notice that because the sum is required to be zero, any values of $p_-1$ and $p_1$ result in the same restricted random distribution once $p_0$ is set.






        share|improve this answer









        $endgroup$

















          4












          $begingroup$

          You need more specificity. @kglr and @Alan give two possibilities (equal probabilities of partitions that sum to zero and (generally) equal numbers of -1's, 0's, and +1's, respectively).



          If you want to consider a model where for any particular sample comes from a multinomial distribution with probabilities $p_-1$, $p_0$, and $p_1$ (subject to $p_-1+p_0+p_1=1$ and $0<p_-1<1$ and $0<p_0<1$ and $0<p_1<1$) and under the restriction that the sum of $n$ samples is zero can be found in the following manner:



          (* Suppose we have the following: *)
          n = 100;
          p0 = 1/2;

          (* Generate a number of zeros such that n - n0 is even and n0 is not negative *)
          SeedRandom[12345];
          n0 = -1;
          While[n0 < 0 || OddQ[n - n0], n0 = RandomVariate[BinomialDistribution[n, p0]]];

          (* The number of -1's and 1's much be each equal to (n-n0)/2 *)
          x = If[n0 == 0, RandomSample[Join[ConstantArray[-1, n/2], ConstantArray[1, n/2]]],
          If[n0 == n, ConstantArray[0, n],
          RandomSample[Join[ConstantArray[-1, (n - n0)/2], ConstantArray[0, nZero],
          ConstantArray[1, (n - n0)/2]]]]];
          x
          (* 0,1,0,-1,1,0,0,-1,1,0,1,-1,1,0,0,-1,0,0,1,0,1,-1,0,-1,0,-1,0,0,0,
          1,1,-1,0,0,1,0,0,-1,1,0,1,1,1,-1,-1,0,1,1,0,1,1,1,0,0,-1,1,0,0,0,0,-1,
          -1,-1,1,0,-1,0,0,0,1,1,-1,0,-1,1,1,-1,0,-1,0,0,0,0,-1,0,-1,0,0,0,0,-1,
          0,-1,-1 *)

          Total[x]
          (* 0 *)


          Notice that because the sum is required to be zero, any values of $p_-1$ and $p_1$ result in the same restricted random distribution once $p_0$ is set.






          share|improve this answer









          $endgroup$















            4












            4








            4





            $begingroup$

            You need more specificity. @kglr and @Alan give two possibilities (equal probabilities of partitions that sum to zero and (generally) equal numbers of -1's, 0's, and +1's, respectively).



            If you want to consider a model where for any particular sample comes from a multinomial distribution with probabilities $p_-1$, $p_0$, and $p_1$ (subject to $p_-1+p_0+p_1=1$ and $0<p_-1<1$ and $0<p_0<1$ and $0<p_1<1$) and under the restriction that the sum of $n$ samples is zero can be found in the following manner:



            (* Suppose we have the following: *)
            n = 100;
            p0 = 1/2;

            (* Generate a number of zeros such that n - n0 is even and n0 is not negative *)
            SeedRandom[12345];
            n0 = -1;
            While[n0 < 0 || OddQ[n - n0], n0 = RandomVariate[BinomialDistribution[n, p0]]];

            (* The number of -1's and 1's much be each equal to (n-n0)/2 *)
            x = If[n0 == 0, RandomSample[Join[ConstantArray[-1, n/2], ConstantArray[1, n/2]]],
            If[n0 == n, ConstantArray[0, n],
            RandomSample[Join[ConstantArray[-1, (n - n0)/2], ConstantArray[0, nZero],
            ConstantArray[1, (n - n0)/2]]]]];
            x
            (* 0,1,0,-1,1,0,0,-1,1,0,1,-1,1,0,0,-1,0,0,1,0,1,-1,0,-1,0,-1,0,0,0,
            1,1,-1,0,0,1,0,0,-1,1,0,1,1,1,-1,-1,0,1,1,0,1,1,1,0,0,-1,1,0,0,0,0,-1,
            -1,-1,1,0,-1,0,0,0,1,1,-1,0,-1,1,1,-1,0,-1,0,0,0,0,-1,0,-1,0,0,0,0,-1,
            0,-1,-1 *)

            Total[x]
            (* 0 *)


            Notice that because the sum is required to be zero, any values of $p_-1$ and $p_1$ result in the same restricted random distribution once $p_0$ is set.






            share|improve this answer









            $endgroup$



            You need more specificity. @kglr and @Alan give two possibilities (equal probabilities of partitions that sum to zero and (generally) equal numbers of -1's, 0's, and +1's, respectively).



            If you want to consider a model where for any particular sample comes from a multinomial distribution with probabilities $p_-1$, $p_0$, and $p_1$ (subject to $p_-1+p_0+p_1=1$ and $0<p_-1<1$ and $0<p_0<1$ and $0<p_1<1$) and under the restriction that the sum of $n$ samples is zero can be found in the following manner:



            (* Suppose we have the following: *)
            n = 100;
            p0 = 1/2;

            (* Generate a number of zeros such that n - n0 is even and n0 is not negative *)
            SeedRandom[12345];
            n0 = -1;
            While[n0 < 0 || OddQ[n - n0], n0 = RandomVariate[BinomialDistribution[n, p0]]];

            (* The number of -1's and 1's much be each equal to (n-n0)/2 *)
            x = If[n0 == 0, RandomSample[Join[ConstantArray[-1, n/2], ConstantArray[1, n/2]]],
            If[n0 == n, ConstantArray[0, n],
            RandomSample[Join[ConstantArray[-1, (n - n0)/2], ConstantArray[0, nZero],
            ConstantArray[1, (n - n0)/2]]]]];
            x
            (* 0,1,0,-1,1,0,0,-1,1,0,1,-1,1,0,0,-1,0,0,1,0,1,-1,0,-1,0,-1,0,0,0,
            1,1,-1,0,0,1,0,0,-1,1,0,1,1,1,-1,-1,0,1,1,0,1,1,1,0,0,-1,1,0,0,0,0,-1,
            -1,-1,1,0,-1,0,0,0,1,1,-1,0,-1,1,1,-1,0,-1,0,0,0,0,-1,0,-1,0,0,0,0,-1,
            0,-1,-1 *)

            Total[x]
            (* 0 *)


            Notice that because the sum is required to be zero, any values of $p_-1$ and $p_1$ result in the same restricted random distribution once $p_0$ is set.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 7 hours ago









            JimBJimB

            19.1k12863




            19.1k12863





















                2












                $begingroup$

                If you want to sample uniformly from all possible tuples that sum to 0, you can do the following:



                zero[n_] := With[

                ones = RandomChoice[
                Table[Multinomial[i,i,n-2i], i,0,Floor[n/2]] -> Range[0,Floor[n/2]]
                ]
                ,

                RandomSample @ PadRight[
                Join[ConstantArray[1,ones],ConstantArray[-1,ones]],
                n
                ]
                ]


                For example, here's a tally of the random 4-tuples summing to 0:



                SeedRandom[1];
                Tally @ Table[zero[4], 10^5]



                1, 1, -1, -1, 5215, -1, 1, 0, 0, 5353, -1, -1, 1, 1,
                5381, 1, -1, 0, 0, 5167, 1, -1, -1, 1, 5169, 0, 1, 0, -1,
                5189, 0, -1, 1, 0, 5311, -1, 1, -1, 1, 5263, 0, -1, 0, 1,
                5376, 0, 0, 1, -1, 5268, 1, 0, -1, 0, 5303, 0, 0, 0, 0,
                5218, 1, 0, 0, -1, 5220, 1, -1, 1, -1, 5095, 0, 0, -1, 1,
                5245, -1, 0, 0, 1, 5313, -1, 1, 1, -1, 5253, 0, 1, -1, 0,
                5264, -1, 0, 1, 0, 5397




                Looks pretty close to uniform sampling.



                As a comparison, note the distribution using the accepted answer:



                nonuniform[n_] := RandomSample @ PadRight[
                Flatten@ConstantArray[1,-1,RandomChoice[Range[0,Floor[n/2]]]],
                n
                ]

                Tally @ Table[nonuniform[4], 10^5]



                -1, 0, 1, 0, 2739, 0, 0, 0, 0, 33695, 0, 1, 0, -1,
                2771, -1, -1, 1, 1, 5682, 0, 0, 1, -1, 2807, 1, -1, 0, 0,
                2697, 0, 0, -1, 1, 2790, -1, 0, 0, 1, 2765, -1, 1, 0, 0,
                2738, 0, -1, 0, 1, 2654, -1, 1, -1, 1, 5482, 1, -1, -1, 1,
                5555, 0, -1, 1, 0, 2768, 1, 0, -1, 0, 2721, -1, 1, 1, -1,
                5519, 1, 1, -1, -1, 5512, 0, 1, -1, 0, 2727, 1, 0, 0, -1,
                2710, 1, -1, 1, -1, 5668




                Not very uniform.






                share|improve this answer











                $endgroup$

















                  2












                  $begingroup$

                  If you want to sample uniformly from all possible tuples that sum to 0, you can do the following:



                  zero[n_] := With[

                  ones = RandomChoice[
                  Table[Multinomial[i,i,n-2i], i,0,Floor[n/2]] -> Range[0,Floor[n/2]]
                  ]
                  ,

                  RandomSample @ PadRight[
                  Join[ConstantArray[1,ones],ConstantArray[-1,ones]],
                  n
                  ]
                  ]


                  For example, here's a tally of the random 4-tuples summing to 0:



                  SeedRandom[1];
                  Tally @ Table[zero[4], 10^5]



                  1, 1, -1, -1, 5215, -1, 1, 0, 0, 5353, -1, -1, 1, 1,
                  5381, 1, -1, 0, 0, 5167, 1, -1, -1, 1, 5169, 0, 1, 0, -1,
                  5189, 0, -1, 1, 0, 5311, -1, 1, -1, 1, 5263, 0, -1, 0, 1,
                  5376, 0, 0, 1, -1, 5268, 1, 0, -1, 0, 5303, 0, 0, 0, 0,
                  5218, 1, 0, 0, -1, 5220, 1, -1, 1, -1, 5095, 0, 0, -1, 1,
                  5245, -1, 0, 0, 1, 5313, -1, 1, 1, -1, 5253, 0, 1, -1, 0,
                  5264, -1, 0, 1, 0, 5397




                  Looks pretty close to uniform sampling.



                  As a comparison, note the distribution using the accepted answer:



                  nonuniform[n_] := RandomSample @ PadRight[
                  Flatten@ConstantArray[1,-1,RandomChoice[Range[0,Floor[n/2]]]],
                  n
                  ]

                  Tally @ Table[nonuniform[4], 10^5]



                  -1, 0, 1, 0, 2739, 0, 0, 0, 0, 33695, 0, 1, 0, -1,
                  2771, -1, -1, 1, 1, 5682, 0, 0, 1, -1, 2807, 1, -1, 0, 0,
                  2697, 0, 0, -1, 1, 2790, -1, 0, 0, 1, 2765, -1, 1, 0, 0,
                  2738, 0, -1, 0, 1, 2654, -1, 1, -1, 1, 5482, 1, -1, -1, 1,
                  5555, 0, -1, 1, 0, 2768, 1, 0, -1, 0, 2721, -1, 1, 1, -1,
                  5519, 1, 1, -1, -1, 5512, 0, 1, -1, 0, 2727, 1, 0, 0, -1,
                  2710, 1, -1, 1, -1, 5668




                  Not very uniform.






                  share|improve this answer











                  $endgroup$















                    2












                    2








                    2





                    $begingroup$

                    If you want to sample uniformly from all possible tuples that sum to 0, you can do the following:



                    zero[n_] := With[

                    ones = RandomChoice[
                    Table[Multinomial[i,i,n-2i], i,0,Floor[n/2]] -> Range[0,Floor[n/2]]
                    ]
                    ,

                    RandomSample @ PadRight[
                    Join[ConstantArray[1,ones],ConstantArray[-1,ones]],
                    n
                    ]
                    ]


                    For example, here's a tally of the random 4-tuples summing to 0:



                    SeedRandom[1];
                    Tally @ Table[zero[4], 10^5]



                    1, 1, -1, -1, 5215, -1, 1, 0, 0, 5353, -1, -1, 1, 1,
                    5381, 1, -1, 0, 0, 5167, 1, -1, -1, 1, 5169, 0, 1, 0, -1,
                    5189, 0, -1, 1, 0, 5311, -1, 1, -1, 1, 5263, 0, -1, 0, 1,
                    5376, 0, 0, 1, -1, 5268, 1, 0, -1, 0, 5303, 0, 0, 0, 0,
                    5218, 1, 0, 0, -1, 5220, 1, -1, 1, -1, 5095, 0, 0, -1, 1,
                    5245, -1, 0, 0, 1, 5313, -1, 1, 1, -1, 5253, 0, 1, -1, 0,
                    5264, -1, 0, 1, 0, 5397




                    Looks pretty close to uniform sampling.



                    As a comparison, note the distribution using the accepted answer:



                    nonuniform[n_] := RandomSample @ PadRight[
                    Flatten@ConstantArray[1,-1,RandomChoice[Range[0,Floor[n/2]]]],
                    n
                    ]

                    Tally @ Table[nonuniform[4], 10^5]



                    -1, 0, 1, 0, 2739, 0, 0, 0, 0, 33695, 0, 1, 0, -1,
                    2771, -1, -1, 1, 1, 5682, 0, 0, 1, -1, 2807, 1, -1, 0, 0,
                    2697, 0, 0, -1, 1, 2790, -1, 0, 0, 1, 2765, -1, 1, 0, 0,
                    2738, 0, -1, 0, 1, 2654, -1, 1, -1, 1, 5482, 1, -1, -1, 1,
                    5555, 0, -1, 1, 0, 2768, 1, 0, -1, 0, 2721, -1, 1, 1, -1,
                    5519, 1, 1, -1, -1, 5512, 0, 1, -1, 0, 2727, 1, 0, 0, -1,
                    2710, 1, -1, 1, -1, 5668




                    Not very uniform.






                    share|improve this answer











                    $endgroup$



                    If you want to sample uniformly from all possible tuples that sum to 0, you can do the following:



                    zero[n_] := With[

                    ones = RandomChoice[
                    Table[Multinomial[i,i,n-2i], i,0,Floor[n/2]] -> Range[0,Floor[n/2]]
                    ]
                    ,

                    RandomSample @ PadRight[
                    Join[ConstantArray[1,ones],ConstantArray[-1,ones]],
                    n
                    ]
                    ]


                    For example, here's a tally of the random 4-tuples summing to 0:



                    SeedRandom[1];
                    Tally @ Table[zero[4], 10^5]



                    1, 1, -1, -1, 5215, -1, 1, 0, 0, 5353, -1, -1, 1, 1,
                    5381, 1, -1, 0, 0, 5167, 1, -1, -1, 1, 5169, 0, 1, 0, -1,
                    5189, 0, -1, 1, 0, 5311, -1, 1, -1, 1, 5263, 0, -1, 0, 1,
                    5376, 0, 0, 1, -1, 5268, 1, 0, -1, 0, 5303, 0, 0, 0, 0,
                    5218, 1, 0, 0, -1, 5220, 1, -1, 1, -1, 5095, 0, 0, -1, 1,
                    5245, -1, 0, 0, 1, 5313, -1, 1, 1, -1, 5253, 0, 1, -1, 0,
                    5264, -1, 0, 1, 0, 5397




                    Looks pretty close to uniform sampling.



                    As a comparison, note the distribution using the accepted answer:



                    nonuniform[n_] := RandomSample @ PadRight[
                    Flatten@ConstantArray[1,-1,RandomChoice[Range[0,Floor[n/2]]]],
                    n
                    ]

                    Tally @ Table[nonuniform[4], 10^5]



                    -1, 0, 1, 0, 2739, 0, 0, 0, 0, 33695, 0, 1, 0, -1,
                    2771, -1, -1, 1, 1, 5682, 0, 0, 1, -1, 2807, 1, -1, 0, 0,
                    2697, 0, 0, -1, 1, 2790, -1, 0, 0, 1, 2765, -1, 1, 0, 0,
                    2738, 0, -1, 0, 1, 2654, -1, 1, -1, 1, 5482, 1, -1, -1, 1,
                    5555, 0, -1, 1, 0, 2768, 1, 0, -1, 0, 2721, -1, 1, 1, -1,
                    5519, 1, 1, -1, -1, 5512, 0, 1, -1, 0, 2727, 1, 0, 0, -1,
                    2710, 1, -1, 1, -1, 5668




                    Not very uniform.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited 5 hours ago

























                    answered 6 hours ago









                    Carl WollCarl Woll

                    84.6k3108218




                    84.6k3108218





















                        1












                        $begingroup$

                        Given the lack of details in your specification, I suspect the following very simple approach will be adequate to your needs:



                        zeroSum[n_] := With[
                        n3 = Floor[n/3]
                        ,
                        RandomSample@Catenate@
                        ConstantArray[-1, n3],
                        ConstantArray[1, n3],
                        ConstantArray[0, n - 2*n3]
                        ]





                        share|improve this answer









                        $endgroup$

















                          1












                          $begingroup$

                          Given the lack of details in your specification, I suspect the following very simple approach will be adequate to your needs:



                          zeroSum[n_] := With[
                          n3 = Floor[n/3]
                          ,
                          RandomSample@Catenate@
                          ConstantArray[-1, n3],
                          ConstantArray[1, n3],
                          ConstantArray[0, n - 2*n3]
                          ]





                          share|improve this answer









                          $endgroup$















                            1












                            1








                            1





                            $begingroup$

                            Given the lack of details in your specification, I suspect the following very simple approach will be adequate to your needs:



                            zeroSum[n_] := With[
                            n3 = Floor[n/3]
                            ,
                            RandomSample@Catenate@
                            ConstantArray[-1, n3],
                            ConstantArray[1, n3],
                            ConstantArray[0, n - 2*n3]
                            ]





                            share|improve this answer









                            $endgroup$



                            Given the lack of details in your specification, I suspect the following very simple approach will be adequate to your needs:



                            zeroSum[n_] := With[
                            n3 = Floor[n/3]
                            ,
                            RandomSample@Catenate@
                            ConstantArray[-1, n3],
                            ConstantArray[1, n3],
                            ConstantArray[0, n - 2*n3]
                            ]






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 7 hours ago









                            AlanAlan

                            6,7921125




                            6,7921125



























                                draft saved

                                draft discarded
















































                                Thanks for contributing an answer to Mathematica Stack Exchange!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                Use MathJax to format equations. MathJax reference.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f200597%2frandominteger-with-equal-number-of-1-and-1%23new-answer', 'question_page');

                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

                                Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

                                François Viète Contents Biography Work and thought Bibliography See also Notes Further reading External links Navigation menup. 21Google Bookspp. 75–77Google BooksDe thou (from University of Saint Andrews)ArchivedGoogle BooksGoogle BooksGoogle BooksGoogle booksGoogle Bookscc-parthenay.frL'histoire universelle (fr)Universal History (en)ArchivedAdsabs.harvard.eduPagesperso-orange.frArchive.orgChikara Sasaki. Descartes' mathematical thought p.259Google BooksGoogle BooksGoogle Bookspp. 152 and onwardGoogle BooksGoogle BooksScribd.comGoogle Books1257-7979Google BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGallica.bnf.frGoogle BooksGoogle Books"François Viète"Francois Viète: Father of Modern Algebraic NotationThe Lawyer and the GamblerAbout TarporleySite de Jean-Paul GuichardL'algèbre nouvelle"About the Harmonicon"cb120511976(data)1188044800000 0001 0913 5903n82164680ola2013766880073431702w6vt1sb70287374827140948071409480