random range

  • Thread starter Charles Comstock
  • Start date
C

Charles Comstock

Why doesn't rand take an integer range and then generate a random number
in between the two?

Example:
rand(5..10) -> a random number from 5 to 10 including 10
rand(5...10) -> a random number from 5 to 10 excluding 10

That or maybe add rand to range so it picks a random item out of the
range. For that matter why doesn't Array/Hash have a method to return a
random element?
That way [1,2,4].rand and (5..10).rand would work.

Charles Comstock
 
H

Hal Fulton

Charles said:
Why doesn't rand take an integer range and then generate a random number
in between the two?

Example:
rand(5..10) -> a random number from 5 to 10 including 10
rand(5...10) -> a random number from 5 to 10 excluding 10

That or maybe add rand to range so it picks a random item out of the
range. For that matter why doesn't Array/Hash have a method to return a
random element?
That way [1,2,4].rand and (5..10).rand would work.

Charles,

We were talking about this in IRC. Maybe you were one of those? :)

Someone suggested allowing not just rand(Fixnum) and rand(Range)
but in general rand(Enumerable).

Someone else said: That's kind of "overloading" the meaning of rand.
Well, I can see that might not be appropriate.

I think if it were my decision, I'd do something like:

1. Let rand accept a (numeric) Range, since rand is already a numeric
kind of thing.
2. Add pick to Enumerable to pick a random element. (See #3)
3. Add pick! also, to do a pick with deletion. (rand! is not an
intuitive name)

Comments?

Hal
 
J

Joel VanderWerf

Hal said:
Someone suggested allowing not just rand(Fixnum) and rand(Range)
but in general rand(Enumerable).

Would be tricky for general Enumerables...

class Seq
include Enumerable
def each
i = 0
loop do
yield i
i += 1
end
end
end
 
P

Phil Tomson

Charles said:
Why doesn't rand take an integer range and then generate a random number
in between the two?

Example:
rand(5..10) -> a random number from 5 to 10 including 10
rand(5...10) -> a random number from 5 to 10 excluding 10

That or maybe add rand to range so it picks a random item out of the
range. For that matter why doesn't Array/Hash have a method to return a
random element?
That way [1,2,4].rand and (5..10).rand would work.

Charles,

We were talking about this in IRC. Maybe you were one of those? :)

Someone suggested allowing not just rand(Fixnum) and rand(Range)
but in general rand(Enumerable).

Someone else said: That's kind of "overloading" the meaning of rand.
Well, I can see that might not be appropriate.

I think if it were my decision, I'd do something like:

1. Let rand accept a (numeric) Range, since rand is already a numeric
kind of thing.
2. Add pick to Enumerable to pick a random element. (See #3)
3. Add pick! also, to do a pick with deletion. (rand! is not an
intuitive name)

I like this a lot. Please submit an RCR.

Phil
 
C

Charles Comstock

Charles,

We were talking about this in IRC. Maybe you were one of those? :)

Someone suggested allowing not just rand(Fixnum) and rand(Range)
but in general rand(Enumerable).

Someone else said: That's kind of "overloading" the meaning of rand.
Well, I can see that might not be appropriate.

I think if it were my decision, I'd do something like:

1. Let rand accept a (numeric) Range, since rand is already a numeric
kind of thing.
2. Add pick to Enumerable to pick a random element. (See #3)
3. Add pick! also, to do a pick with deletion. (rand! is not an
intuitive name)

Comments?

Hal
Well I wasn't in the IRC channel, it gets filtered on my connection, but
I am glad others have thought of using rand in that fashion. I think
those 3 systems make logical sense. I would have the pick command have
some argument to choose what sort of distribution though, defaulting to
uniform distribution. Not sure if it makes more sense to specify these
as constants or as passed blocks or something. I would be worried about
the use of pick though on some things though. Also how would you notate
that pick! had removed an item from a range? It makes sense for sets,
arrays, hashes, but does it make sense for a numeric range? Definitely
have pick for Enumerable, but pick! may have some issues.

Charles Comstock
 

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
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top