Am 13.03.2012 19:00, schrieb Aditya Mahajan: > On Tue, 13 Mar 2012, Peter Rolf wrote: > >> Am 13.03.2012 15:45, schrieb Thomas A. Schmitz: >>> Hi all, >>> >>> scenario: for a presentation, I want the background to be a random >>> picture, taken from a directory "backgroundpics." The pictures are >>> numbered consecutively as 1.jpg through 22.jpg. For some complex >>> reasons, it would be easier for me to include these pictures via metafun >>> (I want to add other graphical elements on top). So my question is, how >>> can I pick a random picture? Given the code >>> >>> StartPage ; >>> draw externalfigure "backgoundpics/1" xscaled PaperWidth yscaled >>> PaperHeight ; >>> StopPage ; >>> >>> how can I obtain a random number and make metapost pick the >>> corresponding picture? Sorry, difficult to make a minimal example >>> here... >>> >> (slightly tested) >> >> string mypath, rndfile; >> >> mypath:= "backgroundpics/"; >> nofpics:= 22; >> >> n:= round(uniformdeviate(nofpics-1)+1); > > Actually, round (uniformdeviate(nopics-1)) does not give equal weightage > to 1 and nofpics. A better solution is > > n:= floor(uniformdeviate(nofpics)) + 1; > :-)
I used 'floor' in my prior tests, but changed it to 'round' because of the very small chance of getting the highest number (at least with my formula). Let's hope, nobody has wikified my bad maths ;-) > which gives a uniform weight to all numbers. > >> rndfile:= mypath & decimal n; > > Aditya > ___________________________________________________________________________________ > > If your question is of interest to others as well, please add an entry > to the Wiki! > > maillist : ntg-context@ntg.nl / > http://www.ntg.nl/mailman/listinfo/ntg-context > webpage : http://www.pragma-ade.nl / http://tex.aanhet.net > archive : http://foundry.supelec.fr/projects/contextrev/ > wiki : http://contextgarden.net > ___________________________________________________________________________________ > > ___________________________________________________________________________________ If your question is of interest to others as well, please add an entry to the Wiki! maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context webpage : http://www.pragma-ade.nl / http://tex.aanhet.net archive : http://foundry.supelec.fr/projects/contextrev/ wiki : http://contextgarden.net ___________________________________________________________________________________