# Exclude zero when sampling random integers

I want to generate triplets of random integers over where the integers are in the range -9 to 9 (inclusive) but excluding zero. How can I do that?

I have the following table.

Table[a -> RandomInteger[{-9, 9}, 3], {50}] // Column

{a -> {0, 9, 6}},
{a -> {7, -3, 1}},
{a -> {-8, -2, -6}},
{a -> {9, 0, 2}},
{a -> {-2, 5, 6}},
{a -> {0, 4, -2}},
{a -> {8, -5, -1}},
{a -> {3, -4, -9}},
{a -> {-8, 7, 6}},
{a -> {-9, 8, 5}},
{a -> {2, -1, 9}},
{a -> {7, 0, 2}},
{a -> {5, -2, 0}},
{a -> {1, -9, -6}},
{a -> {0, -8, 0}},
{a -> {-9, -9, -2}},
{a -> {9, -2, -3}},
{a -> {6, -1, -5}},
{a -> {-3, 2, 1}},
{a -> {-7, 3, 0}},
{a -> {8, 4, -5}},
{a -> {-9, -1, 0}},
{a -> {1, 0, -4}},
{a -> {0, -4, 3}},
{a -> {-1, -9, -6}},
{a -> {9, 8, -3}},
{a -> {6, -2, 4}},
{a -> {2, 3, 8}},
{a -> {-9, 1, -4}},
{a -> {8, -1, -9}},
{a -> {-7, 7, 9}},
{a -> {-8, -7, 2}},
{a -> {2, 3, -7}},
{a -> {-2, 9, 5}},
{a -> {9, 1, 0}},
{a -> {8, 0, -6}},
{a -> {-4, -8, 1}},
{a -> {8, -5, 6}},
{a -> {-9, -4, -9}},
{a -> {9, 7, -8}},
{a -> {9, 8, -5}},
{a -> {9, 5, 5}},
{a -> {-4, -5, -4}},
{a -> {-3, 2, 0}},
{a -> {-9, -9, 0}},
{a -> {-7, 1, -4}},
{a -> {7, 6, -5}},
{a -> {-4, 1, -6}},
{a -> {-5, -9, 0}},
{a -> {-6, 2, -6}}


I don't want the zeros that appear above. I have used DeleteCasesand Replace with no results,

• How about Table[a -> Table[(-1)^RandomInteger[{1, 2}] RandomInteger[{1, 9}], {3}], {50}] // Column
– user46676
May 27, 2017 at 22:15
• @jose check the solution i posted below if you are interested in using DeleteCases and Replace May 28, 2017 at 12:31
• How to remove repeated elements
– jose
May 28, 2017 at 15:17

try

Table[a -> RandomChoice[Join[-Range[9], Range[9]], 3], 50] // Column

• RandomSample takes 3 distinct choices. It should be RandomChoice. May 27, 2017 at 22:36
• Thanks, I'm left with this solution v / s the random
– jose
May 27, 2017 at 22:54
• How to remove repeated elements
– jose
May 28, 2017 at 15:17
• you mean DeleteDuplicates@ ? May 28, 2017 at 15:22
• I tried with DeleteDuplicates but the list is reduced to 3 to 2 elements I need it to be 3 elements
– jose
May 28, 2017 at 18:27

Replace

RandomInteger[{-9, 9}, 3]


with

RandomChoice[Delete[Range[-9, 9], 10], 3]


### Update

The OP asks in a comment below that the selected triples be constrained so that no element triples is duplicated. I would enforce that constraint with a recursive helper command.

With[{items = Delete[Range[-9, 9], 10]},
helper :=
Module[{triple},
triple = DeleteDuplicates @ RandomSample[items, 3];
If[Length[triple] == 3, triple, helper]]]

SeedRandom[42]; Column[rules = Table[a -> helper, 50]]

a -> {2, 9, -9}
a -> {5, -3, -6}
a -> {9, 7, 8}
...
a -> {4, -2, -6}
a -> {-1, -7, 9}
a -> {-7, 9, -5}


The following proves the above result contains no triple with duplicate elements.

AllTrue[rules, Length[Union[#[[2]]]] === 3 &]


True

• RandomChoice[{-1, 1}, 3] RandomInteger[9, 3] should work, too. May 27, 2017 at 22:13
• @J.M. That should be RandomInteger[{1,9}, 3] (I also thought it starts at 1, but it turns out it starts at 0.) May 27, 2017 at 22:35
• How to remove repeated elements
– jose
May 28, 2017 at 15:18
• Instead of Delete referring to a position, I'd probably use either Cases[Range[-9, 9], Except[0]] or DeleteCases[Range[-9, 9], 0]. Jun 3, 2017 at 14:48
• @kirma. Seems obvious to me that in a sequence ranging from -n to n, the zero term is always the n + 1-th element. Why not use that fact? Jun 3, 2017 at 18:48

If you are interested in distinct triples, you might want to generate the entire list of possibilities and then use RandomSample:

samples = RandomSample[Tuples[DeleteCases[Range[-9,9],0], 3], 50];


{a->{-1,3,5},a->{5,-9,-7},a->{8,6,-6},<<44>>,a->{-9,-8,-1},a->{2,-1,9},a->{8,-9,-6}}

randomSigns[] := RandomChoice[{1, -1}, {3}]
randomNumber[n_] := RandomInteger[{1, 9}, {n, 3}]
a -> randomSigns[] # & /@ randomNumber[50]


This will delete all entries with zeros:

t = Table[a -> RandomInteger[{-9, 9}, 3], {50}];
DeleteCases[t, HoldPattern[a -> p : {__}] /; MemberQ[p, 0]]


also this will replace entries where zeros are present:

replacements = Range[-9, -1]~Join~Range[1, 9];
Replace[t, HoldPattern[a -> {OrderlessPatternSequence[(p : 0 ..), (t : Except[0] ..)]}] :>
a -> {t}~Join~RandomChoice[replacements, Length@{p}], {1}]


Demo:

t // Short
(* {a -> {8, -2, -1}, a -> {8, 6, 0}, a -> {9, 5, -4}, a -> {9, 0, 0},
a -> {-7, 0, 4}, a -> {8, -5, -6}, a -> {4, 7, 2}, a -> {0, 3, 2}, <<35>>,
a -> {-5, -1, -6}, a -> {5, 7, -5}, a -> {-2, -3, 3}, a -> {-4, 0, 4},
a -> {1, 7, 3}, a -> {0, 9, -1}, a -> {-7, -4, -4}} *)

DeleteCases[t, HoldPattern[a -> p : {__}] /; MemberQ[p, 0]]//Short
(* {a -> {8, -2, -1}, a -> {9, 5, -4}, a -> {8, -5, -6}, a -> {4, 7, 2},
a -> {8, 5, -7}, a -> {5, 2, 3}, a -> {-5, 9, -5}, a -> {1, 5, -8}, <<22>>,
a -> {-9, -6, -7}, a -> {-4, 3, -9}, a -> {-5, -1, -6},
a -> {5, 7, -5}, a -> {-2, -3, 3}, a -> {1, 7, 3}, a -> {-7, -4, -4}} *)

(* if we need to keep the length of the list and replace entries where zero(s) exist *)

replacements = Range[-9, -1]~Join~Range[1, 9];
Replace[t, HoldPattern[a -> {OrderlessPatternSequence[(p : 0 ..),(t : Except[0] ..)]}] :>
a -> {t}~Join~ RandomChoice[replacements, Length@{p}], {1} ] // Short

(* {a -> {8, -2, -1}, a -> {6, 8, 2}, a -> {9, 5, -4}, a -> {9, -6, 3},
a -> {-7, 4, 8}, a -> {8, -5, -6}, a -> {4, 7, 2}, a -> {2, 3, -5},
<<35>>, a -> {-5, -1, -6}, a -> {5, 7, -5}, a -> {-2, -3, 3},
a -> {-4, 4, 5}, a -> {1, 7, 3}, a -> {-1, 9, -8}, a -> {-7, -4, -4}} *)

• How to remove repeated elements, thanks for help
– jose
May 28, 2017 at 15:18
• @jose sorry i am not able to understand. The code i posted in my solution either removes zeros or replaces them if found. What do you mean by repeated elements ?? May 28, 2017 at 15:53
• @jose if what you mean is removing identical entries then you would need to apply DeleteDuplicates on the above results May 28, 2017 at 15:55