# Getting the 557th element of a table

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?

-
Look at Take in the documentation – R. M. Oct 3 '12 at 21:59
Sounds more like Part is what he is looking for. – Mr Alpha Oct 3 '12 at 22:01
My professor gave the hint: use "Select" – user43523 Oct 3 '12 at 22:04
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 :) – R. M. Oct 3 '12 at 22:11
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. – murray Oct 4 '12 at 0:25

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 *)
x[[3]]
(* ==> 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}
Select[
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.)

-
That Select method is just silly... Go Syntactic sugar! – kale Oct 4 '12 at 0:45