help me name this function

Discussion in 'C Programming' started by copx, Feb 22, 2011.

  1. copx

    copx Guest

    What's a good name for a random number function which
    receives an int (n) and returns a random int
    (uniform distribution) 0..n-1?

    That's quite useful in C. For example to choose a random
    array element like:

    element = array[rng_n(ARRAY_SIZE)]

    rng_n() is the current name of my function, I am trying
    to find a more meaningful name...
    copx, Feb 22, 2011
    #1
    1. Advertising

  2. copx

    ec429 Guest

    On 22/02/11 23:02, copx wrote:
    > What's a good name for a random number function which
    > receives an int (n) and returns a random int
    > (uniform distribution) 0..n-1?
    >
    > That's quite useful in C. For example to choose a random array element
    > like:
    >
    > element = array[rng_n(ARRAY_SIZE)]
    >
    > rng_n() is the current name of my function, I am trying
    > to find a more meaningful name...
    >
    >

    I have two suggestions for you:
    1) unsigned int discrete_uniform(unsigned int);
    2) off_t opic(int his_newsgroup); /* need to #include <sys/types.h> */
    -E
    --
    'sane', adj.: see 'unimaginative'
    on the web - http://jttlov.no-ip.org
    ec429, Feb 22, 2011
    #2
    1. Advertising

  3. copx

    copx Guest

    "ec429" wrote in message news:ik1fuf$5dd$-september.org...
    >I have two suggestions for you:
    >1) unsigned int discrete_uniform(unsigned int);


    Unfortunately, my rng lib has multiple functions which
    are based on discrete uniform distribution, in fact all functions
    are based on it and thus this needs no explicit mentioning.
    I am mostly trying to find a good expression for "0..n-1" here.
    copx, Feb 22, 2011
    #3
  4. "copx" <> wrote:
    > What's a good name for a random number function which
    > receives an int (n) and returns a random int
    > (uniform distribution) 0..n-1?


    I call mine randN().

    --
    Peter
    Peter Nilsson, Feb 23, 2011
    #4
  5. Sherm Pendley <> writes:
    > "copx" <> writes:
    >> "ec429" wrote in message
    >> news:ik1fuf$5dd$-september.org...
    >>>I have two suggestions for you:
    >>>1) unsigned int discrete_uniform(unsigned int);

    >>
    >> Unfortunately, my rng lib has multiple functions which
    >> are based on discrete uniform distribution, in fact all functions
    >> are based on it and thus this needs no explicit mentioning. I am
    >> mostly trying to find a good expression for "0..n-1" here.

    >
    > dN(), based on the traditional RPG die expressions of d6, d10, d20,
    > etc.


    Not bad -- except that RGP dice are typically 1-based (d6 is 1..6,
    etc.).

    I think I've seen the name "randn" for this kind of thing.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Feb 23, 2011
    #5
  6. copx

    copx Guest

    "Keith Thompson" wrote in message news:...
    >I think I've seen the name "randn" for this kind of thing.


    I guess I will use rng_n() then ("rng_" is the namespace prefix here).
    I kinda hoped there was a technical term for "0..n-1".
    copx, Feb 23, 2011
    #6
  7. copx

    copx Guest

    "Sherm Pendley" wrote in message news:...
    >> mostly trying to find a good expression for "0..n-1" here.

    >
    >dN(), based on the traditional RPG die expressions of d6, d10, d20,
    >etc.


    I don't see the logic behind that, "d6" means 1..6, not 0..5.
    Also I already have a random dice funtion: rng_dice(n_dice, n_sides)
    in the same module/namespace.
    copx, Feb 23, 2011
    #7
  8. "copx" <> writes:

    > "Keith Thompson" wrote in message
    > news:...
    >>I think I've seen the name "randn" for this kind of thing.

    >
    > I guess I will use rng_n() then ("rng_" is the namespace prefix
    > here). I kinda hoped there was a technical term for "0..n-1".


    Well in a way there is. If the function's return type is unsigned it is
    simply "less than n". Another closely related technical term is "modulo
    n". (This does not rule out the other equivalence classes as being the
    returned set (for example n..2n-1) but 0..n-1 is considered canonical.)

    When a function takes one argument, I am not averse to using a name that
    refers to the argument, so I'd consider things like 'rand_less_than' and
    'rng_mod' because they read reasonably well. ('mod' is a well-known
    contraction of 'modulo'.)

    --
    Ben.
    Ben Bacarisse, Feb 23, 2011
    #8
  9. On Feb 23, 1:02 am, "copx" <> wrote:
    >
    > rng_n() is the current name of my function, I am trying
    > to find a more meaningful name...
    >

    randi. It's reasonably short, and the i indicates that it's integral.
    Everyone expects a uniform distribution, so you don't need to spell it
    out in the name.

    I use uniform() for a random float 0 - 1-epsilon and coin() for a
    boolean 0 or 1, though I return it as an int.
    Malcolm McLean, Feb 23, 2011
    #9
  10. copx

    copx Guest

    Re: help me name this function rng_mod(AREA_WIDTH) rng_less_than(AREA_WIDTH)

    "Ben Bacarisse" wrote in message
    news:...
    >When a function takes one argument, I am not averse to using a name that
    >refers to the argument, so I'd consider things like 'rand_less_than' and
    >'rng_mod' because they read reasonably well. ('mod' is a well-known
    >contraction of 'modulo'.)


    Thanks, those are some good suggestions. I will use rng_mod(n), problem
    solved!
    copx, Feb 23, 2011
    #10
  11. copx

    copx Guest

    "Ben Bacarisse" wrote in message
    news:...

    >When a function takes one argument, I am not averse to using a name that
    >refers to the argument, so I'd consider things like 'rand_less_than' and
    >'rng_mod' because they read reasonably well. ('mod' is a well-known
    >contraction of 'modulo'.)


    Thanks, those are some good suggestions. I will use rng_mod(n), problem
    solved!
    copx, Feb 23, 2011
    #11
  12. copx

    copx Guest

    Re: help me name this function rng_mod(AREA_WIDTH) rng_less_than(AREA_WIDTH)

    "copx" wrote in message news:ik3q0v$ota$...

    [snip]

    Please ignore, mispost.
    copx, Feb 23, 2011
    #12
  13. In article <ik1f9n$qum$>, "copx" <>
    wrote:

    > What's a good name for a random number function which
    > receives an int (n) and returns a random int
    > (uniform distribution) 0..n-1?
    >
    > That's quite useful in C. For example to choose a random
    > array element like:
    >
    > element = array[rng_n(ARRAY_SIZE)]
    >
    > rng_n() is the current name of my function, I am trying
    > to find a more meaningful name...


    udizn();

    Uniformly distributed integers on zero to n.

    --
    Michael Press
    Michael Press, Feb 24, 2011
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?iso-8859-1?B?bW9vcJk=?=
    Replies:
    7
    Views:
    817
    Roedy Green
    Jan 2, 2006
  2. ding feng
    Replies:
    2
    Views:
    2,801
    ding feng
    Jun 25, 2003
  3. Bobby Chamness
    Replies:
    2
    Views:
    2,388
    Joe Smith
    Apr 22, 2007
  4. Jack-2
    Replies:
    3
    Views:
    262
    Jack-2
    Dec 24, 2003
  5. Java  script  Dude

    IE name="name" & form.name property bug

    Java script Dude, Jun 29, 2004, in forum: Javascript
    Replies:
    5
    Views:
    237
    Java script Dude
    Jun 30, 2004
Loading...

Share This Page