1
$\begingroup$

Okay, so, I'm trying to write a program within the module. It goes like this: create empty list, join it with a sample from a certain set called MyList, and then repeat it certain number of times. Sounds simple.

Ex[m_] := Module[
  {sample={}, i = 0},
  While[i < m,
  sample= Join[sample, RandomSample[MyList, 7]];
  i++]
sample
  ]

So, Ex[1], should give me just seven numbers, Ex[2] fourteen, etc. But no. It would be too simple. Ex[1] gives me {10 Null, 12 Null, 31 Null, 2 Null, 38 Null, 17 Null, 7 Null}, and so on. Why? What are those nulls everywhere? Have I definied sample wrong? {} is an empty list, quite obviously. Typing manually

Join[{}, {1, 2, 3, 4, 5, 6, 7}]

Gives me no errors.

$\endgroup$
4
  • 1
    $\begingroup$ It is a good idea to start all of your symbols with lowercase letters: ex, myList. $\endgroup$
    – gwr
    Commented Feb 6, 2016 at 15:47
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$
    – Michael E2
    Commented Feb 6, 2016 at 16:14
  • 2
    $\begingroup$ You're missing a ; after your While loop. Note that your while loop basically does what Table does (but Table is more efficient and, more importantly imo, more readable). $\endgroup$
    – Michael E2
    Commented Feb 6, 2016 at 16:16
  • $\begingroup$ Also: (17808) $\endgroup$
    – Mr.Wizard
    Commented Feb 6, 2016 at 17:57

1 Answer 1

1
$\begingroup$

when I include the semicolon and the parentheses it seems to work in my Mathematica session like so:

MyList = {1, 2, 3, 4, 5, 6, 7}


Ex[m_] :=  Module[
  {sample = {}, i = 0},
    (While[i < m, 
    sample = Join[sample, RandomSample[MyList, 7]];
    i++];
   sample) 
]

Ex[3]

{5, 1, 4, 3, 7, 2, 6, 4, 3, 2, 1, 7, 6, 5, 6, 1, 2, 7, 5, 4, 3}

$\endgroup$

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