Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2004-09

Random(), Randomize, and Linux permissions

31 messages · 13 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Alan Mead

It's known that Randomize() is not effective when called rapidly... I tried to solve this using the code sample below. I can cat /dev/null (without becoming root) but when I run the code below I get an access denied RTE at 'Reset(f,1);'.. this code works fine when run as root... anyone have a suggestion?

-Alan

  procedure SuperRandomize;
    var
      f: file of cardinal;
      Card: Cardinal;
    begin
        Assign(f,'/dev/random');
        Reset(f,1);
        read(f,Card);
        RandSeed := Card;
        close(f);
    end;
Vincent Snijders
to Alan Mead
Alan Mead wrote
It's known that Randomize() is not effective when called rapidly...

If you call randomize in the same process, I don't know, why you should call randomize more than once. But maybe you are calling randomize in seperate processes ...

Vincent.

Florian Klaempfl
to Alan Mead: It's known that Randomize() is not effective when called rapidly... I tried to solve this…
Alan Mead wrote
It's known that Randomize() is not effective when called rapidly... I tried to solve this using the code sample below. I can cat /dev/null (without becoming root) but when I run the code below I get an access denied RTE at 'Reset(f,1);'.. this code works fine when run as

/dev/random isn't effective either when called permanently. The entropy pool of /dev/random is limited and /dev/random is a blocking device thus it will block after several thousand calls. /dev/urandom doesn't block but it's pseudo random as soon as /dev/random runs out of entropy.

root... anyone have a suggestion?
Do a filemode:=0; before.
Show quoted text 18 lines
-Alan procedure SuperRandomize; var f: file of cardinal; Card: Cardinal; begin Assign(f,'/dev/random'); Reset(f,1); read(f,Card); RandSeed := Card; close(f); end; _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Jilani Khaldi
to Jonas Maebe

Hi All,
Does somebody kown the algorithm to convert a date from the double format to an explicit format?
Example: the function "now" givers me: 3.825956615197917E+004 I want to transform it in: year-month-day....

Thanks!

Jilani

Michael Van Canneyt
to Jilani Khaldi
Jilani Khaldi wrote 5 lines
Hi All, Does somebody kown the algorithm to convert a date from the double format to an explicit format? Example: the function "now" givers me: 3.825956615197917E+004 I want to transform it in: year-month-day....

Why not use DateToStr() or FormatDateTime() from sysutils ?

Michael.

Alan Mead
to Jonas Maebe: Why do you want to call randomize rapidly? Reading a numbers from /dev/random is…

Thanks for all the suggestions.

Right, if my program were to execute cntinuously while it generated these random numbers, it would be easy to just call randomize once. But my program runs very quickly and I'd like it to produce very random numbers even upon repeated execution of the program. If nothing else, it's important for my testing of it. As it stands, if I run it repeatedy without much time between runs I don't just get similar numbers, I seem to get the exact same sequence. I didn't time it precisely, but I'd say you want about a second or more to elapse between calls to Randomize() in order to get different sequences.

Hence my attempt to use /dev/urandom. I had suggested this in a discussion on this list about a year ago and I thought I would try to implement it now... I acknowledge the limitations of this approach.. maybe it would be easier to just stick a 1 sec delay before the randomize :)

BTW, I meant to ask: IIRC, about a year ago someone mentioned that 2.0 would use the Mersenne Twister method. Is that what's in 1.9.4? Last time I compared FPC's random() to MT, MT was a clear winner.

-Alan

--- Jonas Maebe wrote 19 lines
On 29 sep 2004, at 08:36, Alan Mead wrote: > It's known that Randomize() is not effective when called rapidly... Why do you want to call randomize rapidly? Reading a numbers from /dev/random is semantically closer to calling random() a lot than to calling randomize() a lot. Jonas _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Anton Tichawa
to Michael Van Canneyt: Why not use DateToStr() or FormatDateTime() from sysutils ? Michael.
Michael Van Canneyt wrote 23 lines
On Wed, 29 Sep 2004, Jilani Khaldi wrote: >Hi All, >Does somebody kown the algorithm to convert a date from the double >format to an explicit format? >Example: the function "now" givers me: 3.825956615197917E+004 >I want to transform it in: year-month-day.... > > Why not use DateToStr() or FormatDateTime() from sysutils ? Michael. _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Or, if you need the values for some sort of calculation, you have to

- find out the reference date, i. e. DateToStr(0)
- and the scale, (I think 1.0 = 1 day)
- consider the month lengths, with february changing in leap years
- for historic dates, consider calendar switches

There's a conversion function somewhere on my hard disk. If I find it, I'll post it later.

Or you might scan the strings created by DateToStr. That'd be quick-n-dirty, though :-)

Anton.

Marco van de Voort
to Anton Tichawa
Show quoted text 16 lines
Michael Van Canneyt wrote: > > Or, if you need the values for some sort of calculation, you have to - find out the reference date, i. e. DateToStr(0) - and the scale, (I think 1.0 = 1 day) - consider the month lengths, with february changing in leap years - for historic dates, consider calendar switches There's a conversion function somewhere on my hard disk. If I find it, I'll post it later. Or you might scan the strings created by DateToStr. That'd be quick-n-dirty, though :-)

It's a lot simpler to use simply SysUtils.DecodeDate and DeccdeTime.

Uses SysUtils;
var Year, Month, Day, hour,minute,second,msec : word;
begin
  decodedate(trunc(thetdatetime),year,month,day);
  decodetime(frac(thetdatetime),hour,minute,second,msec);
end;
Michalis Kamburelis
to Anton Tichawa: Or, if you need the values for some sort of calculation, you have to - find out the…
Anton Tichawa wrote 40 lines
Michael Van Canneyt wrote: > On Wed, 29 Sep 2004, Jilani Khaldi wrote: > > > >> Hi All, >> Does somebody kown the algorithm to convert a date from the double >> format to an explicit format? >> Example: the function "now" givers me: 3.825956615197917E+004 >> I want to transform it in: year-month-day.... >> > > > > Why not use DateToStr() or FormatDateTime() from sysutils ? > > Michael. > > _______________________________________________ > fpc-pascal maillist - > http://lists.freepascal.org/mailman/listinfo/fpc-pascal > > > Or, if you need the values for some sort of calculation, you have to - find out the reference date, i. e. DateToStr(0) - and the scale, (I think 1.0 = 1 day) - consider the month lengths, with february changing in leap years - for historic dates, consider calendar switches There's a conversion function somewhere on my hard disk. If I find it, I'll post it later. Or you might scan the strings created by DateToStr. That'd be quick-n-dirty, though :-) Anton.

Or, simpler, use DecodeDateTime function from DateUtils unit. It's even Delphi-compatible. I'm not sure whether it exists in FPC 1.0.x (even if not, it's probably possible to just compile DateUtils unit sources from FPC 1.9.4 using FPC 1.0.10), but it exists in FPC 1.9.x for sure.

Michalis
Marco van de Voort
to Michalis Kamburelis
Show quoted text 10 lines
> Or you might scan the strings created by DateToStr. That'd be > quick-n-dirty, though :-) > > Anton. > Or, simpler, use DecodeDateTime function from DateUtils unit. It's even Delphi-compatible. I'm not sure whether it exists in FPC 1.0.x (even if not, it's probably possible to just compile DateUtils unit sources from FPC 1.9.4 using FPC 1.0.10), but it exists in FPC 1.9.x for sure.

Correct, except for the detail that 1.9(.0), and maybe 1.9.2 don't have dateutils. It was a later addition to the 1.9.x branch.

At first sight, dateutils seems to not use 1.9.x features, so it could be that it indeed compiles under 1.0.10

Unknown author
to Anton Tichawa: Or, if you need the values for some sort of calculation, you have to - find out the…
Anton Tichawa wrote 34 lines
Michael Van Canneyt wrote: >On Wed, 29 Sep 2004, Jilani Khaldi wrote: > > > >>Hi All, >>Does somebody kown the algorithm to convert a date from the double >>format to an explicit format? >>Example: the function "now" givers me: 3.825956615197917E+004 >>I want to transform it in: year-month-day.... >> >> > > >Why not use DateToStr() or FormatDateTime() from sysutils ? > >Michael. > >_______________________________________________ >fpc-pascal maillist - >http://lists.freepascal.org/mailman/listinfo/fpc-pascal > > > Or, if you need the values for some sort of calculation, you have to - find out the reference date, i. e. DateToStr(0) - and the scale, (I think 1.0 = 1 day) - consider the month lengths, with february changing in leap years - for historic dates, consider calendar switches There's a conversion function somewhere on my hard disk. If I find it, I'll post it later.

The dateutils unit that comes with FPC 1.9.x contains a bazillion conversion routines.

Michael

Jilani Khaldi
to Michalis Kamburelis: Or, simpler, use DecodeDateTime function from DateUtils unit. It's even…
Or, simpler, use DecodeDateTime function from DateUtils unit. It's

even Delphi-compatible. I'm not sure whether it exists in FPC 1.0.x (even if not, it's probably

possible to just compile DateUtils unit sources from FPC 1.9.4 using

FPC 1.0.10), but it exists in FPC 1.9.x for sure.
I know I can use DecodeDateTime, but I am just curious about the algorithm behind this conversion.

Jilani

Jilani Khaldi Architect & Software Developer http://jkhaldi.oltrelinux.com
Jilani Khaldi
to Marco van de Voort: It's a lot simpler to use simply SysUtils.DecodeDate and DeccdeTime. Uses SysUtils; var…

Hi All,
In the next issue of Linux&C n.42 (http://www.oltrelinux.com) there will be the first article (Special) about Free Pascal (8 pages for this article, never happened before with other cool languages). Linux&C is the number one magazione in Italy dedicated to Linux with nearly 50.000 readers monthly. I am looking for a cool application (already written or to be written) to show to our readers (in the next articles) the power and the elegance of Free Pascal for serious development. Please let me know if you know about any. Let's spear Free Pascal! Jilani

Jilani Khaldi Architect & Software Developer http://jkhaldi.oltrelinux.com
Jilani Khaldi
to Marco van de Voort: It's a lot simpler to use simply SysUtils.DecodeDate and DeccdeTime. Uses SysUtils; var…
Show quoted text 12 lines
It's a lot simpler to use simply SysUtils.DecodeDate and DeccdeTime. Uses SysUtils; var Year, Month, Day, hour,minute,second,msec : word; begin decodedate(trunc(thetdatetime),year,month,day); decodetime(frac(thetdatetime),hour,minute,second,msec); end;

Well, I want to use the double value as a key to control a session in a web application, then I want to be able to get the explicit date from this value. So the surfer when he/she sees the number couldn't understand its meaning ;-). How ever, even if I can get the same result using other hints, I am really curious to understand the algorithm (all the frac part!).
Thank you the same.

Jilani

Unknown author
to Jilani Khaldi: even Delphi-compatible. I'm not sure whether it exists in FPC 1.0.x (even if not, it's…
Jilani Khaldi wrote 7 lines
> Or, simpler, use DecodeDateTime function from DateUtils unit. It's even Delphi-compatible. I'm not sure whether it exists in FPC 1.0.x (even if not, it's probably > possible to just compile DateUtils unit sources from FPC 1.9.4 using FPC 1.0.10), but it exists in FPC 1.9.x for sure. I know I can use DecodeDateTime, but I am just curious about the algorithm behind this conversion.

The TDateTime format is explained in the docs. It is a double:
- The integer part is the number of days since 00:00 1.1.1900
- The fractional part is the number of milliseconds since 00:00 of the day divided by the total number of milliseconds in a day.

Michael.

Alan Mead
to Jeff Pohlmeyer: Have you tried this ? http://daniel.taickim.net/development/mtwister.pp…

Jeff,

Thanks for this link. I found a Pascal implementation of MT somewhere .. not this one I think. Is it your code? Do you know the difference between the Real1() and Real2() etc. methods?

I was hoping that the algorithms underlying the RTL.Randomize and RTL.Random would switch to using MT in fpc version 2.

-Alan

--- Jeff Pohlmeyer wrote 19 lines
> Last time I compared FPC's random() to MT, MT was a clear winner. Have you tried this ? http://daniel.taickim.net/development/mtwister.pp __________________________________ Do you Yahoo!? Yahoo! Mail is new and improved - Check it out! http://promotions.yahoo.com/new_mail _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Alan Mead
to Alan Mead
Alan Mead wrote
I was hoping that the algorithms underlying the RTL.Randomize and RTL.Random would switch to using MT in fpc version 2.

Here's the post I recalled:

Is this still the case that 1.9.x implements MT? Hmm. As, I recall, MT beat FPC's 1.x random() in my testing using "ent"... I'll have to take a look again.

-Alan

Jonas Maebe
to Alan Mead: Thanks for all the suggestions. Right, if my program were to execute cntinuously while it…
Alan Mead wrote 5 lines
Hence my attempt to use /dev/urandom. I had suggested this in a discussion on this list about a year ago and I thought I would try to implement it now... I acknowledge the limitations of this approach.. maybe it would be easier to just stick a 1 sec delay before the randomize :)

You could use a number from /dev/random or /dev/urandom to initialize randseed (that's just a global variable in the system unit, which is the only thing that randomize() changes when you call it), instead of randomize(). Then you'll get the best of both worlds.

Jonas

Florian Klaempfl
to Jilani Khaldi: In the next issue of Linux&C n.42 (http://www.oltrelinux.com) there will be the first…
Jilani Khaldi wrote 8 lines
Hi All, In the next issue of Linux&C n.42 (http://www.oltrelinux.com) there will be the first article (Special) about Free Pascal (8 pages for this article, never happened before with other cool languages). Linux&C is the number one magazione in Italy dedicated to Linux with nearly 50.000 readers monthly. I am looking for a cool application (already written or to be written) to show to our readers (in the next articles) the power and the elegance of Free Pascal for serious development.

FPC itself ;)?
Lazarus =) ?
Tony's CBT?

Please let me know if you know about any. Let's spear Free Pascal! Jilani
Dean Zobec
to Jilani Khaldi: In the next issue of Linux&C n.42 (http://www.oltrelinux.com) there will be the first…

Jilani Khaldi,

Hi All, In the next issue of Linux&C n.42 (http://www.oltrelinux.com) there will be the first article (Special) about Free Pascal (8 pages for this article, never happened before with other cool languages)

Good news, I've known a lot of fpc fans here in Italy (I'm from Trieste).

Linux&C is the number one magazine in Italy dedicated to Linux with nearly 50.000 readers monthly.

I've read your articles on FirebirdSQL you wrote for Linux&C, very clear and well written.

I am looking for a cool application (already written or to be written) to show to our readers (in the next articles) the power and the elegance of Free Pascal for serious development.

I agree with Florian, FPC itself! imho is sufficient to look at the fpc documentation to find enough examples of elegance and power. Anyway, a bunch of good Delphi libraries have been successfully ported to fpc (Synapse, ICS, Decal, Abbrevia,...), almost anything you can do with Delphi could be done in fpc as well.
I've converted the EOS code (http://sourceforge.net/projects/camelos) to fpc in a couple of days with only minor changes from Delphi and translated the Junit core testing framework from Java in a day or too: http://camelos.sourceforge.net/fpcUnit.html , so, no doubt for me that FPC is mature for serious programming.

If you need help for your article or you need a review, just ask (you can get in contact with me in private).
Ciao,
Dean Zobec

A.J. Venter
to Florian Klaempfl: FPC itself ;)? Lazarus =) ? Tony's CBT?

Am I allowed to punt my own apps ?

How about direqcafe ?
It's entirely written in lazarus, and is the most widely used thin-client internet cafe management tool available on Linux.
http://akinimod.sf.net

Ciao
A.J.

Florian Klaempfl wrote 21 lines
Jilani Khaldi wrote: > Hi All, > In the next issue of Linux&C n.42 (http://www.oltrelinux.com) there will > be the first article (Special) about Free Pascal (8 pages for this > article, never happened before with other cool languages). Linux&C is > the number one magazione in Italy dedicated to Linux with nearly 50.000 > readers monthly. I am looking for a cool application (already written or > to be written) to show to our readers (in the next articles) the power > and the elegance of Free Pascal for serious development. FPC itself ;)? Lazarus =) ? Tony's CBT? > Please let me > know if you know about any. Let's spear Free Pascal! > Jilani _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
A.J. Venter
to A.J. Venter

Okay, appology - I didn't look closely and thought this came from the lazarus mailing list. I'll leave it to the requester to decide if a lazarus app should also count as kewl fpc app :)

A.J.

A.J. Venter wrote 10 lines
Am I allowed to punt my own apps ? How about direqcafe ? It's entirely written in lazarus, and is the most widely used thin-client internet cafe management tool available on Linux. http://akinimod.sf.net Ciao A.J.
Vincent Snijders
to Dean Zobec: Good news, I've known a lot of fpc fans here in Italy (I'm from Trieste). I've read your…
Dean Zobec wrote
I've converted the EOS code (http://sourceforge.net/projects/camelos) to fpc in a couple of days with only minor changes from Delphi and translated the Junit core testing framework from Java in a day or too: http://camelos.sourceforge.net/fpcUnit.html , so,

I am interested in fpcUnit. Where can I download the source for fpcUnit. The tar.gz on http://camelos.sourceforge.net/fpcUnit.html only contains the gui test runner, not the base classes, like TestCase. I tried to find it at cvs, but couldn't.

Regards,
Vincent.

Unknown author
to Vincent Snijders
Vincent Snijders wrote 11 lines
Dean Zobec wrote: > I've converted the EOS code (http://sourceforge.net/projects/camelos) to fpc > in a couple of days with only minor changes from Delphi and translated the > Junit core testing framework from Java in a day or too: > http://camelos.sourceforge.net/fpcUnit.html , so, I am interested in fpcUnit. Where can I download the source for fpcUnit. The tar.gz on http://camelos.sourceforge.net/fpcUnit.html only contains the gui test runner, not the base classes, like TestCase. I tried to find it at cvs, but couldn't.

I think it would be even good to include it with FPC itself, in the FCL. As far as I know, the test classes themselves don't need any GUI functionality ?

Michael.

Dean Zobec
to Vincent Snijders: I am interested in fpcUnit. Where can I download the source for fpcUnit. The tar.gz on…
Show quoted text 9 lines
> I've converted the EOS code (http://sourceforge.net/projects/camelos) to > fpc in a couple of days with only minor changes from Delphi and > translated the Junit core testing framework from Java in a day or too: > http://camelos.sourceforge.net/fpcUnit.html I am interested in fpcUnit. Where can I download the source for fpcUnit. The tar.gz on http://camelos.sourceforge.net/fpcUnit.html only contains the gui test runner, not the base classes, like TestCase. I tried to find it at cvs, but couldn't.

It's included in the EOS package:
http://prdownloads.sourceforge.net/camelos/EOS-0.9.3.tar.gz?download (in the cvs you'll find it in /EOS/Apps/fpcUnit/ ) as it uses the EOS objects and containers for xml output. Please feel free to contact me for any question about it and any suggestion is highly welcomed.
I'm planning to resume the work on the code to make it EOS independent, and it could be a good idea IMO to include it as a plugin in Lazarus like JUnit in Eclipse, but I don't have enough experience on Lazarus yet. I should work on it for a while, meanwhile any help would be greatly appreciated.

Regards,
Dean

Dean Zobec
to Unknown author: I think it would be even good to include it with FPC itself, in the FCL. As far as I…

Michael,

Show quoted text 13 lines
> > I've converted the EOS code (http://sourceforge.net/projects/camelos) > > to fpc in a couple of days with only minor changes from Delphi and > > translated the Junit core testing framework from Java in a day or too: > > http://camelos.sourceforge.net/fpcUnit.html , so, > > I am interested in fpcUnit. Where can I download the source for fpcUnit. > The tar.gz on http://camelos.sourceforge.net/fpcUnit.html only contains > the gui test runner, not the base classes, like TestCase. I tried to > find it at cvs, but couldn't. I think it would be even good to include it with FPC itself, in the FCL. As far as I know, the test classes themselves don't need any GUI functionality ?

You're right, like in JUnit, no GUI functionality is really needed for the tests, I usually run the tests from the command line, by writing a simple console program like this:

  RegisterTests([TMyFirstTest, TMySecondTest, TMyThirdTest ....]);
  RunRegisteredTests;
  writeln(GetResultsAsXML);

see the tests of the fpcUnit itself for a complete example: http://cvs.sourceforge.net/viewcvs.py/camelos/EOS/Apps/fpcUnit/tests/runtests.dpr?rev=1.10&view=log

I'll be very happy to see it included in the FCL as a humble contribution to my preferred compiler. For the occasion I could write a command line template program for the running of the tests and insert some command line arguments to be able to choose different formats for the results (xml or plain text for example).
The code ha a dependency on the EOS library for the automatic xml serialization that is probably unnecessary and I think I can easily remove it. I can send you a streamlined FPC version by the end of the month for an evaluation.

Thank you again for your constant effort in the FPC development. Regards,
Dean

Unknown author
to Dean Zobec
Dean Zobec wrote 30 lines
Michael, > > > I've converted the EOS code (http://sourceforge.net/projects/camelos) > > > to fpc in a couple of days with only minor changes from Delphi and > > > translated the Junit core testing framework from Java in a day or too: > > > http://camelos.sourceforge.net/fpcUnit.html , so, > > > > I am interested in fpcUnit. Where can I download the source for fpcUnit. > > The tar.gz on http://camelos.sourceforge.net/fpcUnit.html only contains > > the gui test runner, not the base classes, like TestCase. I tried to > > find it at cvs, but couldn't. > > I think it would be even good to include it with FPC itself, in the FCL. > As far as I know, the test classes themselves don't need any GUI > functionality ? You're right, like in JUnit, no GUI functionality is really needed for the tests, I usually run the tests from the command line, by writing a simple console program like this: RegisterTests([TMyFirstTest, TMySecondTest, TMyThirdTest ....]); RunRegisteredTests; writeln(GetResultsAsXML); see the tests of the fpcUnit itself for a complete example: http://cvs.sourceforge.net/viewcvs.py/camelos/EOS/Apps/fpcUnit/tests/runtests.dpr?rev=1.10&view=log I'll be very happy to see it included in the FCL as a humble contribution to my preferred compiler. For the occasion I could write a command line template program for the running of the tests and insert some command line arguments to be able to choose different formats for the results (xml or plain text for example).

I would be grateful for that.

The code ha a dependency on the EOS library for the automatic xml serialization that is probably unnecessary and I think I can easily remove it. I can send you a streamlined FPC version by the end of the month for an evaluation.

Please do !
The XML can be generated by our own XML units, if you want XML.

As soon as I get that, I will add it to the FCL.

Thank you again for your constant effort in the FPC development.

And thank you for contributing !

Michael.