Subject | Re: [firebird-support] Rand function in ib_udf |
---|---|
Author | Walter Ogston |
Post date | 2004-06-25T14:37:51Z |
Ivan,
Thanks, you confirm what I suspected was going on (after sleeping on the
problem). I write Delphi 7. I could probably figure out how to write and
install my own udf, but a step-by-step would save me a bunch of time. I
think the steps are:
1. Write a library in delphi using the Random() function from the System
unit. I guess the call to Randomize goes in the initialization
section. The seed is kept in a global variable, which is not strictly
thread-safe but should cause no problem.
2. Copy the .dll file into the udf folder along with fbudf.dll and ib_udf.dll
3. Drop old declaration of rand in database, and re-define referring to
the new dll. It looks to me that Firebird already knows the path to the
udf folder, but I don't see where this is set. Is it a hard-coded default?
4. Once it is working, send the code to someone for possible inclusion in
the next release.
Any comments, traps, suggestions?
Walter
At 10:40 PM 6/24/2004 +0200, you wrote:
C. Walter Ogston
ogstoncw@... Kalamazoo, Michigan
*/
Thanks, you confirm what I suspected was going on (after sleeping on the
problem). I write Delphi 7. I could probably figure out how to write and
install my own udf, but a step-by-step would save me a bunch of time. I
think the steps are:
1. Write a library in delphi using the Random() function from the System
unit. I guess the call to Randomize goes in the initialization
section. The seed is kept in a global variable, which is not strictly
thread-safe but should cause no problem.
2. Copy the .dll file into the udf folder along with fbudf.dll and ib_udf.dll
3. Drop old declaration of rand in database, and re-define referring to
the new dll. It looks to me that Firebird already knows the path to the
udf folder, but I don't see where this is set. Is it a hard-coded default?
4. Once it is working, send the code to someone for possible inclusion in
the next release.
Any comments, traps, suggestions?
Walter
At 10:40 PM 6/24/2004 +0200, you wrote:
>Exactly, rand() UDF in ib_udf is completely broken./*----------------
>The seed is set each time it is called !!
>and is set from time() function which has 1 second precision.
>So, the current function of rand() is that it returns
>encrypted time :-/
>
>What programming language do you use ?
>It is simple to write your own and working udf.
>
>Ivan
>
>
>----- Original Message -----
>From: "Walter Ogston" <ogstoncw@...>
>To: <firebird-support@yahoogroups.com>
>Sent: Thursday, June 24, 2004 10:07 PM
>Subject: [firebird-support] Rand function in ib_udf
>
>
> > Fear friends,
> >
> > I am new at Firebird, udfs and stored procs, and using the rand function
> > from ib_udf in a stored procedure defined as follows:
> >
> > SET TERM ^;
> > alter procedure INSERT_KEYS
> > (proj integer, number_of_keys integer)
> > returns (keys_created integer)
> > AS
> > declare variable i integer;
> > BEGIN
> > keys_created = 0;
> > i = number_of_keys;
> > while (i > 0) do
> > begin
> > insert into answersheet (PROJECT_NUMBER, ANSWER_NUMBER)
> > values (proj, Rand() * (999999999 - 100000000) + 100000000);
> > keys_created = keys_created + 1;
> > i = i - 1;
> > end
> > END ^
> > SET TERM ; ^
> >
> > The idea is to create a set of random 9-digit values (cardinality =
> > number_of_keys) for answer_number. When I call this function with
> > number_of_keys = 1 it works fine, but setting number_of_keys >= 2 I get a
> > key violation. Primary key of answersheet relation is (project_number;
> > answer_number).
> >
> > It looks like the udf function Rand is not getting called again but is
> > using a cached return value, or else it is being called with the same seed
> > (according to the documentation in ib_udf.sql "random number generator is
> > seeded using the current time").
> >
> > Source for rand (from
> >
> <http://cvs.sourceforge.net/viewcvs.py/*checkout*/firebird/interbase/extli
> b/i>http://cvs.sourceforge.net/viewcvs.py/*checkout*/firebird/interbase/ex
> tlib/i
> > b_udf.c?rev=1.3) is:
> >
> > double EXPORT IB_UDF_rand() { srand( (unsigned)time (NULL)); return
> > ((float) rand() / (float) RAND_MAX); }
> >
> > Any ideas how to fix or work around this problem?
> >
> > TIA
> >
> > Walter
> > /*----------------
> > C. Walter Ogston
> > ogstoncw@... Kalamazoo, Michigan
> > */
>
>
>Yahoo! Groups Sponsor
>ADVERTISEMENT
>
>
>----------
>Yahoo! Groups Links
> * To visit your group on the web, go to:
> *
> <http://groups.yahoo.com/group/firebird-support/>http://groups.yahoo.com/g
> roup/firebird-support/
> *
> * To unsubscribe from this group, send an email to:
> *
> <mailto:firebird-support-unsubscribe@yahoogroups.com?subject=Unsubscribe>f
> irebird-support-unsubscribe@yahoogroups.com
> *
> * Your use of Yahoo! Groups is subject to the
> <http://docs.yahoo.com/info/terms/>Yahoo! Terms of Service.
C. Walter Ogston
ogstoncw@... Kalamazoo, Michigan
*/