Lines that pass through a cube I have to make a program in which I have a cube (centered at origin) and random lines that pass through the cube. I don't know what condition could be sufficient that assures me that the lines are going to pass through there.
 A: I assume you are already given a line, and you want to know if it passes through the cube. It's not entirely clear from your question if this really suits your needs.
I also assume the line is parameterized by a point $A$ and a vector $\vec u$. That is, a point $M$ of the line satisfies $M=A+\lambda \vec u$ for some real number $\lambda$.
It's relatively easy to determine the points of the line that are on one side of a given plane. This will give you a (possibly infinite) interval for $\lambda$.
For instance, the points above or on the plane $z=0$ correspond to the values of $\lambda$ for which $z_A+\lambda z_u\geq0$.


*

*If $z_u=0$, the interval is empty if $z_A<0$, and $]-\infty,+\infty[$ if $z_A\geq0$.

*If $z_u>0$, the interval is $[-\frac{z_A}{z_u},+\infty[$.

*If $z_u<0$, the interval is $]-\infty,-\frac{z_A}{z_u}]$.


For a plane $z=z_0$, the conditions are just slightly modified, and for planes $x=x_0$ and $y=y_0$, it's trivial to adapt the formulas.
Now, if you do that with the six planes formed with the faces of the cube, you get six intervals. The line passes through the cube iff their intersection is not empty. Since the intersection of two intervals is still an interval, it should be easy to deal with.
I expect this would be easy to work out for any cube with sides parallel to the coordinates axes. And as a bonus, you are able to describe all the points that lie inside the cube.

However, if you want to generate random lines that pass through the cube, this is probably useless to you. And you will be faced with a problem similar to Bertrand's paradox: how you actually choose the random lines will yield different probability distributions. You may want to consider first (and maybe tell us if you want some useful help) what you want to do with these random lines.
A: Pick a random point in the cube and make sure your line goes through that point.
$$(x,y,z) = (x_0,y_0,z_0) + t(a,b,c)$$
$(x_0,y_0,z_0)$ is the point in the cube, and $(a,b,c)$ is the direction of the vector/line.  This works for $\forall a,b,c \in \mathbb{R}^3$
A: [Note: As Jean-Claude pointed out, in a comment, this was incorrect. Now it is incomplete, but perhaps salvageable.]
Here is a suggestion that often, but not always, answers the question of whether a given line passes through the cube. (It never gives a wrong answer, but sometimes it doesn’t answer the question.)
Suppose the side length of the origin-centered cube is $2s$, so the cube extends $s$ units from the origin in three perpendicular directions.
Note first that the origin-centered sphere of radius $s$ lies entirely within the cube, and the cube lies entirely within the origin-centered sphere of radius $s\sqrt{3}$. These observations justify 2. and 3. below.


*

*Find the distance $d$ from the line to the origin. (See here for one way to do this.)

*If $d\le s$, the line passes through the cube.

*If $d>s\sqrt{3}$, the line does not pass through the cube.

*If $s<d\le s\sqrt3$, another approach must be taken to determine if the line passes through the cube. [In my original answer, I said that the line passed through the cube if and only if the point on the line closest to the origin lay inside the cube, but that was incorrect.]
A: Assume that your cube with side $2a$ is centered at origin ($\vec O$). The equation of the line is $\vec {P_1}+\vec{v}t$, with $|\vec v|=1$. The length of the projection of $OP_1$ on the line is given by $(\vec O-\vec{P_1})\cdot \vec v$, so the closest point on the line to the center of the cube is given by $\vec P=\vec{P_1}-(\vec P_1\cdot \vec v)\vec v$.
If the line intersects the cube all three components of $\vec P$ are between $-a$ and $a$. 
A: Let $C:=[{-a},a]^3$ be the cube, and assume that the line $\ell$ is given by a parametrization of the form
$$t\mapsto x_i(t)=p_i+ u_i t\qquad(1\leq i\leq 3)\ .$$
After a suitable reflection of $\ell$ we may assume $u_i\geq0$ $(1\leq i\leq 3)$. The conditions
$$x_i(t)=-a, \quad{\rm resp.,}\quad x_i(t)=a\qquad(1\leq i\leq3)$$
determine three $t$-intervals $[t_i',t_i'']$. If 
$$\max\{t_1',t_2',t_3'\}\leq\min\{t_1'',t_2'',t_3''\}$$
then $\ell$ hits the cube.
A: A uselessly complicated solution:
Draw a random direction uniformly. You can use the formulas from http://mathworld.wolfram.com/SpherePointPicking.html, in spherical coordinates.
Now if you project the cube in this direction, you will see an hexagon. Pick a point randomly inside the hexagon, and together with the chosen direction, you have your line.
To pick the point uniformly in the hexagon, you can use rejection (sample in the bounding box and reject the external points). Or you can decompose the hexagon in four triangles, choose one of the triangles with a probability proportional to its area, and use uniform sampling in this triangle: http://mathworld.wolfram.com/TrianglePointPicking.html.
