The combination of electric vehicles and renewable energy is taking shape as a potential driver for a future free of fossil fuels. However, the efficient management of the electric vehicle fleet is not exempt from challenges. It calls for the involvement of all actors directly or indirectly related to the energy and transportation sectors, ranging from governments, automakers and transmission system operators, to the ultimate beneficiary of the change: the end-user. An electric vehicle is primarily to be used to satisfy driving needs, and accordingly charging policies must be designed primarily for this purpose. The charging models presented in the technical literature, however, overlook the stochastic nature of driving patterns. Here we introduce an efficient stochastic dynamic programming model to optimally charge an electric vehicle while accounting for the uncertainty inherent to its use. With this aim in mind, driving patterns are described by an inhomogeneous Markov model that is fitted using data collected from the utilization of an electric vehicle. We show that the randomness intrinsic to driving needs has a substantial impact on the charging strategy to be implemented.