random:integer vs. random:seeded-integer
Monday, 18 August 2014 · 4 messages · last reply 19 Aug 2014
AG
Amanda Galtman18 Aug 2014 15:56 #
Hi,
Is it intentional that
random:integer lets you use zero or a negative value for $max, but random:seeded-integer errors out in those cases? I know these are edge cases. I'm only flagging them because of the inconsistency between the two similar functions.
(: Returns (0,0). :)
(random:integer(0),random:integer(-5))(: Errors out :)
random:seeded-integer(31,1,0)
(: Errors out :)
random:seeded-integer(31,1,-5)Here is the error information from the last command above.
Error:
Improper use? Potential bug? Your feedback is welcome:
Contact: basex-talk at mailman.uni-konstanz.de
Version: BaseX 7.9
Java: Oracle Corporation, 1.7.0_25
OS: Windows 7, amd64
Stack Trace:
java.lang.IllegalArgumentException: n must be positive
at java.util.Random.nextInt(Unknown Source)
at org.basex.query.func.FNRandom$1.next(FNRandom.java:85)
at org.basex.query.MainModule$1.next(MainModule.java:98)
at org.basex.query.QueryContext.execute(QueryContext.java:598)
at org.basex.query.QueryProcessor.execute(QueryProcessor.java:101)
at org.basex.core.cmd.AQuery.query(AQuery.java:82)
at org.basex.core.cmd.XQuery.run(XQuery.java:22)
at org.basex.core.Command.run(Command.java:329)
at org.basex.core.Command.execute(Command.java:94)
at org.basex.gui.GUI.exec(GUI.java:417)
at org.basex.gui.GUI.access$500(GUI.java:41)
at org.basex.gui.GUI$8.run(GUI.java:361)Cheers,
Amanda
DK
Dirk Kirsten18 Aug 2014 17:10 #
Hello Amanda,
I agree, this behaviour is inconsistent. I am not quite sure what is the best way to go here. Raising no error and simply returning 0 (as it is currently done for
random:integer() if max is out of range) seems pretty dangerous to me, considering that randomness is often used for security and an unexpected 0 might be hazardous.
Raising a Java error is maybe not the most elegant thing, maybe an XQuery error would be more elegant (like FOAR0001, see
https://docs.basex.org/wiki/XQuery_Errors).
I created a small pull request, which also throws an error for random:integer() if max is out of range, see
https://github.com/BaseXdb/basex/pull/985.
Cheers,
DirkQuoted text · Amanda Galtman · 42 lines
On 18/08/14 15:56, Amanda Galtman wrote: > Hi, > > Is it intentional that random:integer lets you use zero or a negative value for $max, but random:seeded-integer errors out in those cases? I know these are edge cases. I'm only flagging them because of the inconsistency between the two similar functions. > > (: Returns (0,0). :) > (random:integer(0),random:integer(-5)) > > (: Errors out :) > random:seeded-integer(31,1,0) > > (: Errors out :) > random:seeded-integer(31,1,-5) > > > Here is the error information from the last command above. > > Error: > Improper use? Potential bug? Your feedback is welcome: > Contact: basex-talk at mailman.uni-konstanz.de > Version: BaseX 7.9 > Java: Oracle Corporation, 1.7.0_25 > OS: Windows 7, amd64 > Stack Trace: > java.lang.IllegalArgumentException: n must be positive > at java.util.Random.nextInt(Unknown Source) > at org.basex.query.func.FNRandom$1.next(FNRandom.java:85) > at org.basex.query.MainModule$1.next(MainModule.java:98) > at org.basex.query.QueryContext.execute(QueryContext.java:598) > at org.basex.query.QueryProcessor.execute(QueryProcessor.java:101) > at org.basex.core.cmd.AQuery.query(AQuery.java:82) > at org.basex.core.cmd.XQuery.run(XQuery.java:22) > at org.basex.core.Command.run(Command.java:329) > at org.basex.core.Command.execute(Command.java:94) > at org.basex.gui.GUI.exec(GUI.java:417) > at org.basex.gui.GUI.access$500(GUI.java:41) > at org.basex.gui.GUI$8.run(GUI.java:361) > > > Cheers, > Amanda >
--
Dirk Kirsten, BaseX GmbH, http://basex.org
|-- Firmensitz: Blarerstrasse 56, 78462 Konstanz
|-- Registergericht Freiburg, HRB: 708285, Geschäftsführer:
| Dr. Christian Grün, Dr. Alexander Holupirek, Michael Seiferle `-- Phone: 0049 7531 28 28 676, Fax: 0049 7531 20 05 22
CG
Christian Grün19 Aug 2014 16:04 #
Hi Amanda,
I have extended Dirk's pull request and added new error codes [1]; a new snapshot is available [2].
Best,
Christian
[1] http://docs.basex.org/wiki/Random_Module
[2] http://files.basex.org/releases/latest/
Quoted text · Dirk Kirsten · 68 lines
On Mon, Aug 18, 2014 at 5:10 PM, Dirk Kirsten <dk at basex.org> wrote: > Hello Amanda, > > I agree, this behaviour is inconsistent. I am not quite sure what is the > best way to go here. Raising no error and simply returning 0 (as it is > currently done for random:integer() if max is out of range) seems pretty > dangerous to me, considering that randomness is often used for security > and an unexpected 0 might be hazardous. > Raising a Java error is maybe not the most elegant thing, maybe an > XQuery error would be more elegant (like FOAR0001, see > https://docs.basex.org/wiki/XQuery_Errors). > > I created a small pull request, which also throws an error for > random:integer() if max is out of range, see > https://github.com/BaseXdb/basex/pull/985. > > Cheers, > Dirk > > On 18/08/14 15:56, Amanda Galtman wrote: >> Hi, >> >> Is it intentional that random:integer lets you use zero or a negative value for $max, but random:seeded-integer errors out in those cases? I know these are edge cases. I'm only flagging them because of the inconsistency between the two similar functions. >> >> (: Returns (0,0). :) >> (random:integer(0),random:integer(-5)) >> >> (: Errors out :) >> random:seeded-integer(31,1,0) >> >> (: Errors out :) >> random:seeded-integer(31,1,-5) >> >> >> Here is the error information from the last command above. >> >> Error: >> Improper use? Potential bug? Your feedback is welcome: >> Contact: basex-talk at mailman.uni-konstanz.de >> Version: BaseX 7.9 >> Java: Oracle Corporation, 1.7.0_25 >> OS: Windows 7, amd64 >> Stack Trace: >> java.lang.IllegalArgumentException: n must be positive >> at java.util.Random.nextInt(Unknown Source) >> at org.basex.query.func.FNRandom$1.next(FNRandom.java:85) >> at org.basex.query.MainModule$1.next(MainModule.java:98) >> at org.basex.query.QueryContext.execute(QueryContext.java:598) >> at org.basex.query.QueryProcessor.execute(QueryProcessor.java:101) >> at org.basex.core.cmd.AQuery.query(AQuery.java:82) >> at org.basex.core.cmd.XQuery.run(XQuery.java:22) >> at org.basex.core.Command.run(Command.java:329) >> at org.basex.core.Command.execute(Command.java:94) >> at org.basex.gui.GUI.exec(GUI.java:417) >> at org.basex.gui.GUI.access$500(GUI.java:41) >> at org.basex.gui.GUI$8.run(GUI.java:361) >> >> >> Cheers, >> Amanda >> > > -- > Dirk Kirsten, BaseX GmbH, http://basex.org > |-- Firmensitz: Blarerstrasse 56, 78462 Konstanz > |-- Registergericht Freiburg, HRB: 708285, Geschäftsführer: > | Dr. Christian Grün, Dr. Alexander Holupirek, Michael Seiferle > `-- Phone: 0049 7531 28 28 676, Fax: 0049 7531 20 05 22
AG
Amanda Galtman19 Aug 2014 16:38 #
Thanks, Christian!
Cheers,
Amanda
Quoted text · Christian Grün · 92 lines
> Date: Tue, 19 Aug 2014 16:04:17 +0200 > From: Christian Gr?n <christian.gruen at gmail.com> > To: Dirk Kirsten <dk at basex.org> > Cc: BaseX <basex-talk at mailman.uni-konstanz.de> > Subject: Re: [basex-talk] random:integer vs. random:seeded-integer > Message-ID: > <CAP94bnObngxdY3GmJABJYsxF8Bt4WJBcyFeQf- > 3QwGfrPJzAOg at mail.gmail.com> > Content-Type: text/plain; charset=ISO-8859-1 > > Hi Amanda, > > I have extended Dirk's pull request and added new error codes [1]; a new > snapshot is available [2]. > > Best, > Christian > > [1] http://docs.basex.org/wiki/Random_Module > [2] http://files.basex.org/releases/latest/ > > On Mon, Aug 18, 2014 at 5:10 PM, Dirk Kirsten <dk at basex.org> wrote: > > Hello Amanda, > > > > I agree, this behaviour is inconsistent. I am not quite sure what is > > the best way to go here. Raising no error and simply returning 0 (as > > it is currently done for random:integer() if max is out of range) > > seems pretty dangerous to me, considering that randomness is often > > used for security and an unexpected 0 might be hazardous. > > Raising a Java error is maybe not the most elegant thing, maybe an > > XQuery error would be more elegant (like FOAR0001, see > > https://docs.basex.org/wiki/XQuery_Errors). > > > > I created a small pull request, which also throws an error for > > random:integer() if max is out of range, see > > https://github.com/BaseXdb/basex/pull/985. > > > > Cheers, > > Dirk > > > > On 18/08/14 15:56, Amanda Galtman wrote: > >> Hi, > >> > >> Is it intentional that random:integer lets you use zero or a negative value > for $max, but random:seeded-integer errors out in those cases? I know > these are edge cases. I'm only flagging them because of the inconsistency > between the two similar functions. > >> > >> (: Returns (0,0). :) > >> (random:integer(0),random:integer(-5)) > >> > >> (: Errors out :) > >> random:seeded-integer(31,1,0) > >> > >> (: Errors out :) > >> random:seeded-integer(31,1,-5) > >> > >> > >> Here is the error information from the last command above. > >> > >> Error: > >> Improper use? Potential bug? Your feedback is welcome: > >> Contact: basex-talk at mailman.uni-konstanz.de > >> Version: BaseX 7.9 > >> Java: Oracle Corporation, 1.7.0_25 > >> OS: Windows 7, amd64 > >> Stack Trace: > >> java.lang.IllegalArgumentException: n must be positive > >> at java.util.Random.nextInt(Unknown Source) > >> at org.basex.query.func.FNRandom$1.next(FNRandom.java:85) > >> at org.basex.query.MainModule$1.next(MainModule.java:98) > >> at org.basex.query.QueryContext.execute(QueryContext.java:598) > >> at org.basex.query.QueryProcessor.execute(QueryProcessor.java:101) > >> at org.basex.core.cmd.AQuery.query(AQuery.java:82) > >> at org.basex.core.cmd.XQuery.run(XQuery.java:22) > >> at org.basex.core.Command.run(Command.java:329) > >> at org.basex.core.Command.execute(Command.java:94) > >> at org.basex.gui.GUI.exec(GUI.java:417) > >> at org.basex.gui.GUI.access$500(GUI.java:41) > >> at org.basex.gui.GUI$8.run(GUI.java:361) > >> > >> > >> Cheers, > >> Amanda > >> > > > > -- > > Dirk Kirsten, BaseX GmbH, http://basex.org > > |-- Firmensitz: Blarerstrasse 56, 78462 Konstanz > > |-- Registergericht Freiburg, HRB: 708285, Gesch?ftsf?hrer: > > | Dr. Christian Gr?n, Dr. Alexander Holupirek, Michael Seiferle > > `-- Phone: 0049 7531 28 28 676, Fax: 0049 7531 20 05 22
Written for BaseX 7.9. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery