%%%%%%%%%S%%%%%%%%%%%%%%%%% Præambel %%%%%%%%%%%%%%%%%%%%%%%%%%%
\typeout{---------- Preamble start -------------} % vises i log

%Dokumenttype og skriftstrrelsSe sættes:
\documentclass[11pt,a4paper]{article} %fjern draft i færdigt dokument. Brug 11pt eller 12pt

% Oplysninger til article-klassen:
\title{Python exercise: Random numbers}
\author{Martin Sparre}
\date{\today }

%linjeafstand:
%\linespread{1.3}

%Laveste niveau i indholdsfortegnelse:
\setcounter{tocdepth}{2}
%Numereringsniveau:
\setcounter{secnumdepth}{2}


%Ligningsnummerering tager formen (section . nr)
%\numberwithin{equation}{section}

%ingen indrykning ved nyt afsnit:
%\parindent=0pt

%Der vises labels i margin - skal kun anvendes i draft
%\usepackage[notref,notcite]{showkeys}

%klikbar indholdsfortegnelse m.m.
%\usepackage{hyperref}

%%%%%%%%%%%%%%%%%%%%%%%%%%% Vigtige Pakker %%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%natbib - en bibTeX-pakke. [Curly] gr, at der bruges {} ved citationer. Natbib skal være før babel
%\usepackage[curly]{natbib}
\usepackage{natbib}

% En række pakker, der skal inkluderes, hvis man skriver på dansk
\usepackage[utf8]{inputenc} %Danske bogstaver kan bruges, ansinew for windoze - latin1 for linux
%\usepackage[danish]{babel} %Kapitler mv. får danske navne
%\usepackage{babel} %Kapitler mv. får danske navne
\usepackage[T1]{fontenc}

%Font:
%\usepackage{lmodern}
\usepackage{palatino}

\usepackage{url}
\usepackage{hyperref}

%\usepackage{sectsty}
%\allsectionsfont{\large\sffamily}

%Marginer:
\usepackage{vmargin}
\setpapersize{A4}
%argumenter:{hleftmargini}{htopmargini}{hrightmargini}{hbottommargini}....
\setmarginsrb{30mm}{20mm}{30mm}{20mm}{12pt}{11mm}{0pt}{11mm}

%fancyhdr - sidehoved og sidefod
\usepackage{fancyhdr}
%Indholdet af sidehoved og sidefod:
\renewcommand{\headheight}{14.5pt} %er obligatorisk v. fancyhdr
\renewcommand{\headrulewidth}{0.5pt}
\renewcommand{\footrulewidth}{0.5pt}
\lhead{Potsdam Uni.} \chead{Random numbers in Python} \rhead{Summer semester 2024}
%\lfoot{bla}
\cfoot{Page \thepage~of \pageref{lastpage}}
%\rfoot{bla}

%En række ams-pakker til matematik:
\usepackage{amsfonts,amsmath,amssymb}

%ntheorem
%\usepackage{amsthm}
\usepackage[amsmath,thmmarks]{ntheorem}
%\theoremsymbol{$\vartriangleleft$}
%\theoremheaderfont{\bfseries\sffamily}
\theorembodyfont{\normalfont}
\newtheorem{Opgave}{Opgave}
%\newtheorem{definition}{Definition}
%\newtheorem{eksempel}{Eksempel}
%\newtheorem{Metode}{Metode}

%inkludering af kildekode:
%\usepackage{verbatim}

%overfull hbox under 3pt ignoreres:
\hfuzz=4pt


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%% Pakker til inkludering af grafik: %%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcommand{\msun}{~\mathrm{M}_{\odot}}

\usepackage{graphicx} 

%pakke så floats ikke flyder, indsæt [H] som argument efter \begin{figur}.
\usepackage{float}

%Denne pakke srger for at floats ikke flyder ind i andre afsnit ved kommandoen \FloatBarrier (dette medfrer ikke ny side i modsætning til \clearpage)
\usepackage{placeins}

%labelfonts i captions bliver fede og captionmargin forstrres:
\usepackage{caption}
\captionsetup{font=small,labelfont=bf}
\setlength{\captionmargin}{20pt}

\pagestyle{fancy}

%%%%%%%%%%%%%%%%%%%%% NYE KOMMANDOER %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%\newcommand{\mc}[1]{\mathcal{#1}}
%\newcommand{\mb}[1]{\mathbf{#1}}

%Horizontale tykke streger via \HRule :
%\newcommand{\HRule}{\rule{\textwidth}{1mm}}

%d'er som bruges ved infinitesimaler:
\DeclareMathOperator{\di}{d\!}

%orddeling
%\hyphenation{Mar-tin}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%% DOKUMENT START %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\typeout{---------- Dokument start -------------} % vises i log
\begin{document}
\section*{Python exercise: Random numbers}

\subsection*{Part I: Generation of random numbers}
\begin{itemize}
\item {\bf RandomNumberGeneration.py} creates $10^7$ random numbers and stores them in a list. Run it, and measure the time it takes to run the code with \bf{time.time()}.
\end{itemize}
Explanation: A list is defined with {\bf ListOfRandomNumbers= []}. Numbers are appended to the list with {\bf ListOfRandomNumbers.append(random.random())} inside a loop ({\bf for i in range(10000000)})

A note on time.time() and UNIX time: On most operating systems, the number of seconds since January 1st 1970 is returned by time.time() -- this is called UNIX time.



\subsection*{Part II: Generation of random numbers with numpy}
\begin{itemize}
\item Run {\bf RandomNumberGeneration\_numpy.py}, which is a NumPy implementation of the same program. We now use the functions {\bf numpy.random.random}, {\bf numpy.max}, {\bf numpy.min} and {\bf numpy.mean}.
\item Modify the program, so it uses {\bf time.time()} to measure the time it takes to draw the random numbers and calculate the min, max and mean.
\end{itemize}


\subsection*{Part III: Calculation of $\pi$ using random numbers}

\begin{itemize}
\item {\bf Pi.py} calculates $\pi$ with python using lists and the random package. Run the program and make sure you understand what is going on.
\item Exercise: Write the same program using NumPy. The following functions and code fragments will be useful: {\bf x=numpy.random.random(10000000)}, {\bf r = numpy.sqrt((x-0.5)**2+(y-0.5)**2)} and {\bf NInsideCircle = x[numpy.where(r$<$0.5)].size}
\item Compare the speed of the two programs. The NumPy program should be around 10 times faster than the python-list program.
\item Explain in text why the program using numpy arrays is faster in comparison to the program using python lists. \emph{Bonus exercise: Try to get ChatGPT to explain this, and check whether you agree with it and understand the response.}
\end{itemize}

%Is numpy arrays better than python lists?
%Can you write a python program that calculates pi using a monte carlo method?
%Can you write an implementation that uses numpy?
%Can you run this python program for me?



\def\aj{AJ}
\def\araa{ARA\&A}
\def\apj{ApJ}
\def\apjl{ApJ}
\def\apjs{ApJS}
\def\apss{Ap\&SS}
\def\aap{A\&A}
\def\aapr{A\&A~Rev.}
\def\aaps{A\&AS}
\def\mnras{MNRAS}
\def\nat{Nature}
\def\pasp{PASP}
\def\aplett{Astrophys.~Lett.}
\def\physrep{Physical Reviews}
\def\nar{New A Rev.}

%\footnotesize{
%\bibliographystyle{mnras}
%\bibliography{ref}
%}

%\section*{Acknowledgements}

%Thanks to Chuck Steidel and Denise Schmitz, who provided inspiration for this exercise, \url{http://www.astro.caltech.edu/~ccs/ay124/ps5.pdf}.



\label{lastpage}
\end{document}
