Make a table with elements $\sin(\sqrt{8 n}/4) + r\, $for $n$ from $1$ to $1000$, where $r$ is a random real number between $-0.1$ and $0.1$ that is different for each value of $n$. Also make a plot of all the points of this table. What is the $557^{th}$ element of this table?

Everything is working well, but I can't get the 557th element of the table. I've tried to use the Select command.

Select[TableSin[Sqrt[(a*n)/4]] + Random[Real, {-0.1, 0.1}], {n, 1, 1000}] ,557]

but it's not working. I can't figure out how to fix it.

Can anyone give me some hints?

  • $\begingroup$ Look at Take in the documentation $\endgroup$
    – rm -rf
    Oct 3, 2012 at 21:59
  • $\begingroup$ Sounds more like Part is what he is looking for. $\endgroup$
    – Mr Alpha
    Oct 3, 2012 at 22:01
  • $\begingroup$ My professor gave the hint: use "Select" $\endgroup$
    – user43523
    Oct 3, 2012 at 22:04
  • 4
    $\begingroup$ And we gave you two more hints... Select would be a really terrible way to do it and I would not advise it. You can probably teach your professor something new today :) $\endgroup$
    – rm -rf
    Oct 3, 2012 at 22:11
  • 1
    $\begingroup$ The problem specifies "different" random reals to be used. Even for such a small list length as 1000, so far as I'm aware there's no guarantee that Mathematica will provide distinct ones. To be extra certain (if it's important), you can take additional measures, e.g.: generate a considerably longer list of random reals; use Union to extract just the distinct entries; and then, if necessary, shorten that result to the 1000 you need. However, the actual intention of the problem statement may have been merely not to use the same randomly chosen number for each item. $\endgroup$
    – murray
    Oct 4, 2012 at 0:25

1 Answer 1


Select is wrong in this case, you can't use it to get the n-th element of a list without using additional helper functions.

You'll get the most out of this exercise by looking at Part, which is a very flexible function for extracting elements out of a list based on their position, for example

x = {1,1,2,3,5,8,13,21,34,55}

(* Explicit syntax. Extracts the 3rd element out of a list
   (first element has index 1). *)
Part[x, 3]
(* ==> 2 *)

(* Syntactic sugar: using [[ ]] for Part *)
(* ==> 2 *)

Part can do much more, e.g. extracting ranges, submatrices and so on. While not necessary to solve this problem, I highly recommend reading a few paragraphs in the Mathematica help - you will need it again soon. :-)

For the fun of it, here's the easiest way of doing the same task with Select instead of Part I could come up with:

x = {1, 1, 2, 3, 5, 8, 13, 21, 34, 55}
    MapIndexed[{#1, First@#2} &, x],
    Last[#] == 3 &
] // First // First
(* ==> 2 *)

(Manually index the list wasting a ton of memory, then pick the element and discard the indexing again.)

  • $\begingroup$ That Select method is just silly... Go Syntactic sugar! $\endgroup$
    – kale
    Oct 4, 2012 at 0:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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