Hey!

How can I do this:

number_pool = (00000…99999).to_a

So the first number is 00000, the next is 00001, 00002 etc

Thank you

On Tue, Nov 26, 2013 at 12:28 PM, Anders F. [email protected]
wrote:

Hey!

How can I do this:

number_pool = (00000…99999).to_a

So the first number is 00000, the next is 00001, 00002 etc.

With strings: ‘00000’…‘99999’.

That was simple. The best way for my application is to do it with
numbers. But if that is not possible, then I will proceed with strings.

Thank you

If you need the leading zeros then it’s not strings you’re after.

FYI, the above array used 28mb of RAM on my PC, so you might be better
off iterating rather than creating the entire array, unless you’re going
to use all of them immediately.

On Nov 26, 2013, at 6:45 AM, Anders F. [email protected] wrote:

That was simple. The best way for my application is to do it with
numbers. But if that is not possible, then I will proceed with strings.

Thank you

Why do you need the leading zeros? If it is for display purposes or
putting into a string then you can still use numbers e.g. (0 … 99_999)
and use one of printf, sprintf, or % to display them:

ratdog:~ mike\$ pry --simple-prompt

n = 123
=> 123

printf ‘%05d’, n
00123=> nil

sprintf ‘%05d’, n
=> “00123”

‘%05d’ % n
=> “00123”

Hope this helps,

Mike

The “`Stok’ disclaimers” apply.

On Tue, Nov 26, 2013 at 12:45 PM, Anders F. [email protected]
wrote:

That was simple. The best way for my application is to do it with

numbers. But if that is not possible, then I will proceed with strings.

A number is a number, it does not have leading zeroes per se. It is an
abstract entity that is represented in base 2 in the computer.

In order to visualize a number in base 10 you need to get a string out
of
it somehow. The string consists of digits. This is so common that #to_s
does that for you and is a method implicitly called in common use cases.

Now, if you want the range to generate integers, you can get a string
with
leading zeroes within a loop this way:

``````"%06d" % integer
``````

But the program has to generate strings at some point or another.

The reason for why I need the leading zeros is: One of the prizes in the
lottery is when the first 3 numbers is the right one.

If the winner number is 00102

Then you also win if your lottery ticket contains 001**

I need the leading zeros later when I have plus and minus to one of the
numbers in the array.

I’m doing a lottery system for school. Where I have to pick random
numbers between 00000 and 99999. After I picked a number it should be
removed from the array. So I thought it was the only way to do this,
even though it uses an awful lot of ram.

On 26 November 2013 12:00, Anders F. [email protected] wrote:

The reason for why I need the leading zeros is: One of the prizes in the
lottery is when the first 3 numbers is the right one.

If the winner number is 00102

Then you also win if your lottery tickets contains 001**

Isn’t that just the same as a

if your_number >= 100 and your_number <= 199
puts “You win!”
end

In that case, wouldn’t you want each number set as an array?
Something like:
[[0, 0, 0], [0, 0, 1], [0, 0, 2]]

Then the index positions would be maintained even if you use 2 digit
numbers.

Joel P. wrote in post #1128660:

In that case, wouldn’t you want each number set as an array?
Something like:
[[0, 0, 0], [0, 0, 1], [0, 0, 2]]

Then the index positions would be maintained even if you use 2 digit
numbers.

I like that idea.

But that will create 99999 arrays. Is that even possible?

Thank you, Joel!

I used some of the things in your code.

Another question. How can I generate a list of numbers, from 0 to 99999,
ending with the same number.
For example if my end number is 6, then my list would be:

00006
00016
00026
00036

99986
99996
etc.

Here’s a quick idea I threw together:

class Lottery

def self.generate
Array.new( 5 ) { rand(9) }
end

def initialize
@previous_winners = []
end

def generate_unique
val = []
begin
val = self.class.generate
end while previous_winners.include? val
@previous_winners << val
val
end

end

class Ticket

attr_accessor :owner

def initialize( owner )
@value = Lottery.generate
self.owner = owner
end

end

irb(main):036:0* t1 = Ticket.new ‘Bob’
=> #<Ticket:0x2fc4f78 @value=[7, 2, 5, 6, 5], @owner=“Bob”>
irb(main):037:0> t2 = Ticket.new ‘Ruth’
=> #<Ticket:0x2f74698 @value=[2, 7, 4, 6, 0], @owner=“Ruth”>
irb(main):038:0> l = Lottery.new
=> #<Lottery:0x2ef1280 @previous_winners=[]>

All you’d need to do is throw all your tickets into an Array,
generate_unique from the Lottery object, and use Array#select to find
the winners:

irb(main):064:0> l.generate_unique
=> [7, 2, 5, 6, 5]

irb(main):072:0> players = t1, t2
=> [#<Ticket:0x2fc4f78 @value=[7, 2, 5, 6, 5], @owner=“Bob”>,
#<Ticket:0x2f74698
@value=[2, 7, 4, 6, 0], @owner=“Ruth”>]
irb(main):073:0> winning_number = [7, 2, 5, 6, 5]
=> [7, 2, 5, 6, 5]
irb(main):074:0> players.select { |player| player.value ==
winning_number }
=> [#<Ticket:0x2fc4f78 @value=[7, 2, 5, 6, 5], @owner=“Bob”>]

Joel P. wrote in post #1128774:

The obvious answer would be to append a 6 to each string.

(‘0000’…‘9999’).map { |n| n + ‘6’ }

Gotta take a look a mapping.

Thanks a lot!

The obvious answer would be to append a 6 to each string.

(‘0000’…‘9999’).map { |n| n + ‘6’ }

Anders F. wrote in post #1128645:

Hey!

How can I do this:

number_pool = (00000…99999).to_a

So the first number is 00000, the next is 00001, 00002 etc

Thank you

n = 67
s_n = n.to_s.rjust(5,“0”)
p s_n

On Nov 27, 2013, at 3:13 AM, Joel P. [email protected] wrote:

The obvious answer would be to append a 6 to each string.

(‘0000’…‘9999’).map { |n| n + ‘6’ }

That might be less obvious to some

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.