Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Rand and timefmt

19 views
Skip to first unread message

D.GH

unread,
Dec 27, 2010, 12:56:37 PM12/27/10
to
Hello,

Is it possible to use the 'time' as an argument to function 'Rand'?

Set timefmt ("% 02H% 02M% 03S")
rand (time)

does not work.

Thank you

Péter Juhász

unread,
Dec 27, 2010, 1:25:37 PM12/27/10
to

What do you want to accomplish?

In gnuplot, a non-zero argument to rand() sets the seeds of the PRNG
algorithm. If you want to set the seeds based on the current time,
something like this may work (on Unix-like systems):

print rand(int(system("date +%s")))

Péter Juhász

sfeam

unread,
Dec 27, 2010, 1:54:25 PM12/27/10
to
D.GH wrote:


What do you mean "does not work"?

You haven't shown us how you loaded the time variable,
but rand(time) should work for any value of time.

Note: timefmt is only used for reading data from an input file,
so I doubt it is relevant to your current use.

D.GH

unread,
Dec 27, 2010, 2:19:35 PM12/27/10
to

My system is Windows XP.
I want to set the seeds based on the current time,
with the command :


print rand(int(system("date +%s")))

I have the following message :
warning: system evaluation not supported by MS-Windows 32 bit
Non-numeric string found where a numeric expression was expected


Dariush

D.GH

unread,
Dec 27, 2010, 2:29:40 PM12/27/10
to


I want to set the seeds based on the current time.
I guess, I can not use the command : rand(time)

Dariush

D.GH

unread,
Dec 27, 2010, 2:38:22 PM12/27/10
to
On 27 déc, 19:54, sfeam <sf...@users.sourceforge.net> wrote:

rand("time") works

Péter Juhász

unread,
Dec 27, 2010, 3:17:56 PM12/27/10
to

So does rand("foo").

D.GH

unread,
Dec 27, 2010, 4:11:50 PM12/27/10
to
I have the following code :

# # # # # # # # # # # # # # # # # # # # # # # # #
set colorbox
r=int(255*rand("date +%01H%02M%03S"));
g=int(255*rand("date +%01H%02M%03S"));
b=int(255*rand("date +%01H%02M%03S"));

MyColor =sprintf("#%02X%02X%02X", r, g, b)
plot sin(x) linecolor rgbcolor MyColor
set term postscript eps color
set out 'figure.eps'
set size 1, 1

# # # # # # # # # # # # # # # # # # # # # # # # #
The goal is to have the graph with a different color each time. The
'figure.eps', whatever the execution still gives a color 'blue'.

Any ideas?

Dariush


Péter Juhász

unread,
Dec 27, 2010, 6:04:33 PM12/27/10
to

This:
r=int(255*rand("date +%01H%02M%03S"));

doesn't make any sense and it wouldn't work for several reasons.

Please use the gnuplot.exe executable instead of the wgnuplot.exe one
because only the former supports execution of system commands.
Unfortunately you need to run an external command to get the current
time because there is no easy way to get it from gnuplot (AFAIK).
Then:

timestr = system("time /t")
time = real(timestr[1:2].timestr[4:5])
r=int(255*rand(time));
g=int(255*rand(0));
b=int(255*rand(0));
...and proceed from there.

Further complication: the writers of Windows have, in their ineffable
wisdom, decided that the "time" command should, when invoked without a
switch, display the current time with a sub-second precision then ask
for the new time. To make it shut up and display just the time without
any accompanying text or prompt, you have to use the /t switch. But
then it displays the hour and minute, but not the second. So your
script will produce the same line colors if ran repeatedly within the
same minute.

Péter Juhász

Péter Juhász

unread,
Dec 27, 2010, 6:23:07 PM12/27/10
to

Somewhat better solution:

timestr = system("echo %TIME%")
time = real(timestr[1:2].timestr[4:5].timestr[7:8].timestr[10:11])
r=int(255*rand(time));
g=int(255*rand(0));
b=int(255*rand(0));
...etc.

Hans-Bernhard Bröker

unread,
Dec 28, 2010, 6:25:38 AM12/28/10
to
On 28.12.2010 00:04, Péter Juhász wrote:

> Please use the gnuplot.exe executable instead of the wgnuplot.exe one
> because only the former supports execution of system commands.

Not exactly correct. The specialized version wgnuplot_pipes.exe also
supports piping --- thus the name.

D.GH

unread,
Dec 28, 2010, 8:23:59 AM12/28/10
to

I tried with the three executables:.

gnuplot.exe , wgnuplot.exe , wgnuplot_pipes

Whatever the executable, I get the impression that Windows XP does not
take into account the following lines:

timestr = system("echo %TIME%")
time = real(timestr[1:2].timestr[4:5].timestr[7:8].timestr[10:11])
r=int(255*rand(time));

g=int(255*rand(time));
b=int(255*rand(time));

Dariush

Hans-Bernhard Bröker

unread,
Dec 28, 2010, 9:02:50 AM12/28/10
to
On 28.12.2010 14:23, D.GH wrote:
> Whatever the executable, I get the impression that Windows XP does not
> take into account the following lines:

And you got that impression ... how? What actually happens?

The output of "show variables" after those commands would be interesting.

D.GH

unread,
Dec 28, 2010, 10:16:41 AM12/28/10
to

The color of the graph does not change

D.GH

unread,
Dec 28, 2010, 10:22:32 AM12/28/10
to
On 28 déc, 15:02, Hans-Bernhard Bröker <HBBroe...@t-online.de> wrote:

The color of the graph does not change.

Here is an example:
######### ##########

set colorbox
set style fill solid border rgb "white"


timestr = system("echo %TIME%");
time = real(timestr[1:2].timestr[4:5].timestr[7:8].timestr[10:11]);
r=int(255*rand(time));
g=int(255*rand(time));
b=int(255*rand(time));

MyColor =sprintf("#%02X%02X%02X", r, g, b)

plot "Mydata.dat" notitle smooth frequency with boxes linecolor
rgbcolor MyColor
set xrange [55.0 : 105.0]
set yrange [0 : 336]
set xtics 65.0,6,95.0 nomirror
set noytics


set term postscript eps color
set out 'figure.eps'
set size 1, 1

set border 1
replot
exit

######### ##########

where Mydata.dat is :

######### Mydata.dat ##########
65.000000 24
72.500000 150
77.500000 335
82.500000 326
87.500000 135
95.000000 30

###############################"

Dariush

Péter Juhász

unread,
Dec 28, 2010, 10:30:42 AM12/28/10
to

By doing this:
r=int(255*rand(time));
g=int(255*rand(time));
b=int(255*rand(time));

you reset the random number generator every time you call the rand()
function. Therefore you'll get the same number every time.

Replace those three lines with these:
dummy = rand(time)
r=int(255*rand(0));
g=int(255*rand(0));
b=int(255*rand(0));

Péter Juhász

D.GH

unread,
Dec 28, 2010, 10:59:07 AM12/28/10
to

Thank you very much, the code works as it should

Dariush

0 new messages