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... Thomas
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...
(Untested) draw externalfigure ("backgroundpics/" & round(uniformdeviate(22)+1)) Aditya
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); rndfile:= mypath & decimal n; HTH, Peter
Thomas ___________________________________________________________________________________
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 ___________________________________________________________________________________
On 03/13/2012 05:33 PM, Peter Rolf wrote:
string mypath, rndfile;
mypath:= "backgroundpics/"; nofpics:= 22;
n:= round(uniformdeviate(nofpics-1)+1); rndfile:= mypath& decimal n;
HTH, Peter
Aditya, Peter, thank you both for your quick replies! Peter's works wonderfully; Aditya's untested code throws an error "! Not implemented: (string)&(known numeric)." I guess this is too arcane for the wiki? Thanks, and best wishes Thomas
On 03/13/2012 05:56 PM, Thomas A. Schmitz wrote:
thank you both for your quick replies! Peter's works wonderfully; Aditya's untested code throws an error "! Not implemented: (string)&(known numeric)." I guess this is too arcane for the wiki?
To clarify: I wasn't talking about the error, which is pretty clear, but about this particular setup - I'm not sure if this is of general interest and should be added to the wiki. Thomas
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; which gives a uniform weight to all numbers.
rndfile:= mypath & decimal n;
Aditya
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 ___________________________________________________________________________________
On 03/13/2012 07:24 PM, Peter Rolf wrote:
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.
Hmm, I absolutely stink at math, but after a few days of testing: the "randomness" is a bit disappointing. All my presentations start more or less with the same set of background pictures, and since most have < 22 slides, some slides never come up in the rotation. Is there a way to provide metafun with a random seed, say the decimal digit of pi corresponding to the rounded number of my cpu's temperature (just kidding)? Or maybe a lua solution would be nice, metafun seems to be a bit to conservative in its choice of random numbers... Thomas
On 15-3-2012 10:18, Thomas A. Schmitz wrote:
On 03/13/2012 07:24 PM, Peter Rolf wrote:
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.
Hmm, I absolutely stink at math, but after a few days of testing: the "randomness" is a bit disappointing. All my presentations start more or less with the same set of background pictures, and since most have < 22 slides, some slides never come up in the rotation. Is there a way to provide metafun with a random seed, say the decimal digit of pi corresponding to the rounded number of my cpu's temperature (just kidding)? Or maybe a lua solution would be nice, metafun seems to be a bit to conservative in its choice of random numbers...
Ah, but keep in mind that the random number is stored in the tuc file! So, as long as that file is not wiped, you get the same random number initialization. Imagine what would happen if we would not do that. So, just erase the tuc if you want another seed. Hans ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com | www.pragma-pod.nl -----------------------------------------------------------------
On 03/15/2012 07:06 PM, Hans Hagen wrote:
Ah, but keep in mind that the random number is stored in the tuc file! So, as long as that file is not wiped, you get the same random number initialization. Imagine what would happen if we would not do that. So, just erase the tuc if you want another seed.
Oops, yes of course, that makes sense - all presentations are generated from the same xml file, so the tuc never changes! After deleting it, I get more of the desired randomness - thanks a lot for the pointer! All best Thomas
participants (4)
-
Aditya Mahajan
-
Hans Hagen
-
Peter Rolf
-
Thomas A. Schmitz