2
$\begingroup$

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,

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

try

Table[a -> RandomChoice[Join[-Range[9], Range[9]], 3], 50] // Column
$\endgroup$
5
  • $\begingroup$ RandomSample takes 3 distinct choices. It should be RandomChoice. $\endgroup$ – Szabolcs May 27 '17 at 22:36
  • $\begingroup$ Thanks, I'm left with this solution v / s the random $\endgroup$ – jose May 27 '17 at 22:54
  • $\begingroup$ How to remove repeated elements $\endgroup$ – jose May 28 '17 at 15:17
  • $\begingroup$ you mean DeleteDuplicates@ ? $\endgroup$ – ZaMoC May 28 '17 at 15:22
  • $\begingroup$ I tried with DeleteDuplicates but the list is reduced to 3 to 2 elements I need it to be 3 elements $\endgroup$ – jose May 28 '17 at 18:27
3
$\begingroup$

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

$\endgroup$
6
  • 2
    $\begingroup$ RandomChoice[{-1, 1}, 3] RandomInteger[9, 3] should work, too. $\endgroup$ – J. M.'s ennui May 27 '17 at 22:13
  • 5
    $\begingroup$ @J.M. That should be RandomInteger[{1,9}, 3] (I also thought it starts at 1, but it turns out it starts at 0.) $\endgroup$ – Szabolcs May 27 '17 at 22:35
  • $\begingroup$ How to remove repeated elements $\endgroup$ – jose May 28 '17 at 15:18
  • $\begingroup$ Instead of Delete referring to a position, I'd probably use either Cases[Range[-9, 9], Except[0]] or DeleteCases[Range[-9, 9], 0]. $\endgroup$ – kirma Jun 3 '17 at 14:48
  • $\begingroup$ @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? $\endgroup$ – m_goldberg Jun 3 '17 at 18:48
2
$\begingroup$

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];
Thread[a -> samples] //Short

{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}}

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

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}} *)
$\endgroup$
3
  • $\begingroup$ How to remove repeated elements, thanks for help $\endgroup$ – jose May 28 '17 at 15:18
  • $\begingroup$ @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 ?? $\endgroup$ – Ali Hashmi May 28 '17 at 15:53
  • $\begingroup$ @jose if what you mean is removing identical entries then you would need to apply DeleteDuplicates on the above results $\endgroup$ – Ali Hashmi May 28 '17 at 15:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.