mainGNU M4 - Support: sr #105800, Random number generator builtin

 
 

sr #105800: Random number generator builtin

Submitter:  None
Submitted:  Fri 16 Mar 2007 07:15:30 AM UTC
   
 
Category:  None Priority:  5 - Normal
Severity:  1 - Wish Status:  None
Privacy:  Public Assigned to:  None
Originator Email:  -email is unavailable- Open/Closed:  Open
Operating System:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Tue 27 Mar 2007 12:06:14 PM UTC, comment #5: 

I never claimed it was perfect :)  Besides, a random number generator that depends on the contents of your disk and creates files in the process is rather weak; I only listed the idea for completeness of what m4 1.4.x already provides.

But yes, to avoid negative numbers, since m4's eval is signed, you would need to add &0x7fffffff in the mix.

Eric Blake <ericb>
Group administrator
Tue 27 Mar 2007 11:21:01 AM UTC, comment #4: 

This solution sometimes gives negative numbers.

Alexander <agaraga>
Mon 26 Mar 2007 04:05:59 PM UTC, comment #3: 

By the way, m4 1.4.x already has a pseudo-random generator: mkstemp (or maketemp, if you want to be portable to 1.4.7 and earlier).  A bit gross, but you could use something along these lines rather than relying on the bash extension of $RANDOM that is not in all versions of /bin/sh; unfortunately it does not reduce the number of forks, and generates only 31 bits (well, 36^6 rather than 2^31) of randomness:

dnl random(num): Evaluates to an unevenly distributed random number from range 0..num-1
define([random],
[pushdef([_], mkstemp([0XXXXXX]))dnl
syscmd([rm ]_)dnl
eval([0r36:]_[ % $1]popdef([_]))])

To be a good random number generator, though, you need an even distribution.  Your approach with printf $RANDOM, as well as my approach above, fails that criteria when you take a modulus that is relatively prime to the number of psuedo-random inputs.  Any good solution that provides random as a builtin must make sure that the resulting distribution is evenly distributed.

Eric Blake <ericb>
Group administrator
Mon 26 Mar 2007 02:20:18 PM UTC, comment #2: 

It's too late to add a new feature to the 1.4.x series (which is supposed to be stable).  Dynamic modules indeed require 2.0 support (well, 1.9x, since 2.0 isn't released yet).  Would you be willing to assign copyright for your contribution?  Otherwise, I will have to reimplement the suggestion from scratch rather than relying on your preliminary work (not that I mind, it just means it will take longer).  Also, in general, it is better to generate a 'diff -u' style patch, rather than to attach the entire contents of the changed file; and preferably make the patch against CVS head rather than a release version, since the file may have changed in CVS since a prior release.  For that matter, patches are more frequently discussed on the m4-patches mailing list than on savannah.  Also, any new builtin also requires documentation and NEWS patches, as well as testsuite additions that exercise the new builtin.

Eric Blake <ericb>
Group administrator
Sat 24 Mar 2007 09:39:58 PM UTC, comment #1: 

The idea sounds useful; I'll probably add something along these lines closer to when M4 2.0 is released.  But the nice part about CVS head (the eventual M4 2.0) is that you can write your own m4 builtins as a dynamic library, and extend the power of the m4 language without having to wait for the next release to implement something as a builtin.

Eric Blake <ericb>
Group administrator
Fri 16 Mar 2007 07:15:30 AM UTC, original submission:  

m4 lacks a random number generator builtin. I think it would be great if such builtin existed. Currently I use something like:
dnl random(num): Evaluates to a random number from range 0..num-1.
define([random],[eval(esyscmd(printf $RANDOM) % $1)])dnl

Anonymous

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

Attached Files
file #12299:  builtin.c added by None (58KiB - text/plain - I've already done it by myself, see the file attached (v.1.4.8). Sorry, I didn't get how write builtins as a dynamic library. Or this is possible in 2.0 version only?)

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by agaraga (Posted a comment)
  • -email is unavailable- added by ericb (Posted a comment)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only logged-in users can vote.

     

    Follows 1 latest change.

    Date Changed by Updated Field Previous Value => Replaced by
    2007-03-26 None Attached File- Added builtin.c, #12299

    Back to the top

    Powered by Savane 3.13-f8d8.
    Corresponding source code