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


sr #105800: Random number generator builtin

Submitted by:  None
Submitted on:  Fri Mar 16 07:15:30 2007  
Category: NonePriority: 5 - Normal
Severity: 1 - WishStatus: None
Privacy: PublicAssigned to: None
Originator Email: -unavailable-Open/Closed: Open
Operating System: None

Add a New Comment (Rich MarkupRich Markup):

You are not logged in

Please log in, so followups can be emailed to you.


Tue Mar 27 12:06:14 2007, 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>
Project Administrator
Tue Mar 27 11:21:01 2007, comment #4:

This solution sometimes gives negative numbers.

Alexander <agaraga>
Mon Mar 26 16:05:59 2007, 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
[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>
Project Administrator
Mon Mar 26 14:20:18 2007, 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>
Project Administrator
Sat Mar 24 21:39:58 2007, 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>
Project Administrator
Fri Mar 16 07:15:30 2007, 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



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

Attach File(s):

Attached Files
file #12299:  builtin.c added by None (58kB - 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
  • -unavailable- added by agaraga (Posted a comment)
  • -unavailable- added by ericb (Posted a comment)

    Do you think this task is very important?
    If so, you can click here to add your encouragement to it.
    This task has 0 encouragements so far.

    Only logged-in users can vote.


    Please enter the title of George Orwell's famous dystopian book (it's a date):



    Follows 1 latest change.

    Date Changed By Updated Field Previous Value => Replaced By
    Mon Mar 26 05:11:05 2007NoneAttached File-=>Added builtin.c, #12299

    Back to the top

    Powered by Savane 3.1-cleanup