help me name this function

C

copx

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...
 
E

ec429

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
 
C

copx

"ec429" wrote in message news:[email protected]...
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.
 
P

Peter Nilsson

copx said:
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().
 
K

Keith Thompson

Sherm Pendley said:
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.
 
C

copx

"Sherm Pendley" wrote in message news:[email protected]...
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.
 
B

Ben Bacarisse

copx said:
"Keith Thompson" wrote in message


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'.)
 
M

Malcolm McLean

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.
 
C

copx

"Ben Bacarisse" wrote in message
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!
 
C

copx

"Ben Bacarisse" wrote in message
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!
 
M

Michael Press

"copx" <[email protected]> said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,834
Latest member
BuyCannaLabsCBD

Latest Threads

Top