Dice Roller

Discussion in 'Ruby' started by Hd Pwnz0r, Sep 7, 2010.

  1. Hd Pwnz0r

    Hd Pwnz0r Guest

    Hd Pwnz0r, Sep 7, 2010
    #1
    1. Advertising

  2. Hd Pwnz0r

    Phlip Guest

    On Sep 6, 5:01 pm, Hd Pwnz0r <> wrote:
    > It won't work. It's odd. It won't stop and it won't add a number onto x
    > so it would say
    > "Roll 1: 5
    > Roll 2: 3"
    > etc
    >
    > It just repeats "Roll 1: (random number)".
    >
    > Also, is there a way for me to add one to the rand function so it won't
    > get a zero?
    >
    > Attachments:http://www.ruby-forum.com/attachment/5009/dice.rb


    Next time just put it here:

    puts "How many dice do you want to roll?"
    roll = gets.chomp
    num_rolls = roll
    y = 0
    x = y.to_i + 1
    while x <= roll.to_i
    puts "Roll " + x.to_s + ": " + rand(max=6).to_s
    end

    Take out the gets.chomp:

    roll = '12' # gets.chomp

    that lets you work on the loop without entering over and over again.

    then either fix your while loop by incrementing: x += 1

    or use an each loop (which is truly the Ruby way):

    (0...roll.to_i).each do |x|
    puts 'etc'
    end
    Phlip, Sep 7, 2010
    #2
    1. Advertising

  3. On Mon, Sep 6, 2010 at 5:01 PM, Hd Pwnz0r
    <> wrote:
    > It won't work. It's odd. It won't stop and it won't add a number onto x
    > so it would say
    > "Roll 1: 5
    > Roll 2: 3"
    > etc
    >
    > It just repeats "Roll 1: (random number)".
    >
    > Also, is there a way for me to add one to the rand function so it won't
    > get a zero?
    >
    > Attachments:
    > http://www.ruby-forum.com/attachment/5009/dice.rb
    >

    I've added comments to your code pointing to areas of concern:

    puts "How many dice do you want to roll?"
    roll = gets.chomp
    num_rolls = roll # Why are you assigning to num_rolls here? You never
    use this variable!
    y = 0 # "y" is a bad name for a variable, generally. What is this
    variable for? Why is it 0?
    x = y.to_i + 1 # What is "x" for? Why are you calling to_i on
    something you just assigned an integer value, so you should know you
    don't need this?
    while x <= roll.to_i # since you never assign to x, if it is ever
    true, it will always be true and you'll never escape from the loop.
    puts "Roll " + x.to_s + ": " + rand(max=6).to_s # you are assigning
    6 to max -- a variable you never use anywhere else -- and then
    immediately passing that to rand. Why?
    end
    Christopher Dicely, Sep 7, 2010
    #3
  4. Hd Pwnz0r

    botp Guest

    On Tue, Sep 7, 2010 at 8:01 AM, Hd Pwnz0r
    <> wrote:
    >puts "How many dice do you want to roll?"
    >roll = gets.chomp
    >num_rolls = roll


    num_rolls = gets.to_i

    >y = 0
    >x = y.to_i + 1


    x = 1

    >while x <= roll.to_i


    while x <= num_rolls

    >puts "Roll " + x.to_s + ": " + rand(max=6).to_s


    puts "Roll #{x} : #{rand(max=6)+1}"
    x += 1

    >end
    >
    > Also, is there a way for me to add one to the rand function so it won't
    > get a zero?


    cmon, a computer cannot add ;-)

    revised code below.

    DICE_SIDES=6
    num_rolls = gets.to_i
    x=1
    while x <= num_rolls
    puts "Roll #{x} : #{rand(DICE_SIDES)+1}"
    x+=1
    end

    =>
    Roll 1 : 3
    Roll 2 : 1
    Roll 3 : 6
    Roll 4 : 2
    Roll 5 : 3

    Welcome to Ruby. Pls do not stop programming!
    best regards -botp
    botp, Sep 7, 2010
    #4
  5. David A. Black wrote:
    > Hi --
    >
    > On Tue, 7 Sep 2010, Phlip wrote:
    >
    >>> get a zero?

    >> while x <= roll.to_i
    >> puts "Roll " + x.to_s + ": " + rand(max=6).to_s
    >> end
    >>
    >> Take out the gets.chomp:
    >>
    >> roll = '12' # gets.chomp
    >>
    >> that lets you work on the loop without entering over and over again.

    >
    > That changes the behavior of the program pretty significantly, though.
    >
    >> then either fix your while loop by incrementing: x += 1
    >>
    >> or use an each loop (which is truly the Ruby way):
    >>
    >> (0...roll.to_i).each do |x|
    >> puts 'etc'
    >> end

    >
    > I'd be inclined to use #times:
    >
    > print "How many dice do you want to roll? "
    > num_rolls = gets.to_i
    > num_rolls.times do |n|
    > puts "Roll #{n+1}: #{rand(6) + 1}"
    > end
    >
    >
    > David
    >
    > --
    > David A. Black, Senior Developer, Cyrus Innovation Inc.
    >
    > The Ruby training with Black/Brown/McAnally
    > Compleat Philadelphia, PA, October 1-2, 2010
    > Rubyist http://www.compleatrubyist.com


    Hi,

    I have made a small change, but both will returns the same result..

    By adding the min value to the rand(max-min), we can get random number
    bw the range(min,max).

    print "How many dice do you want to roll? "
    num_rolls = gets.to_i
    num_rolls.times do |n|
    puts "Roll #{n+1}: #{1 + rand(6-1)}"
    end

    Thanks,
    Vaddi
    --
    Posted via http://www.ruby-forum.com/.
    Vadivelan Vatti, Sep 7, 2010
    #5
  6. Vadivelan Vaddi wrote:
    > By adding the min value to the rand(max-min), we can get random number
    > bw the range(min,max).
    >
    > print "How many dice do you want to roll? "
    > num_rolls = gets.to_i
    > num_rolls.times do |n|
    > puts "Roll #{n+1}: #{1 + rand(6-1)}"
    > end


    That's a five-sided die. rand(5) gives a number between 0 and 4
    inclusive.
    --
    Posted via http://www.ruby-forum.com/.
    Brian Candler, Sep 7, 2010
    #6
  7. Hd Pwnz0r

    vadivelan K Guest

    [Note: parts of this message were removed to make it a legal post.]

    Thanks Brian for pointed out this. Yes it will returns the value bw 0 and 4
    suits for 5-sided die.

    We can use this

    puts "Roll #{n+1}: #{1 + rand(7-1)}"

    This way we can get the values between 0 to 6 as final result.


    On Tue, Sep 7, 2010 at 1:22 PM, Brian Candler <> wrote:

    > Vadivelan Vaddi wrote:
    > > By adding the min value to the rand(max-min), we can get random number
    > > bw the range(min,max).
    > >
    > > print "How many dice do you want to roll? "
    > > num_rolls = gets.to_i
    > > num_rolls.times do |n|
    > > puts "Roll #{n+1}: #{1 + rand(6-1)}"
    > > end

    >
    > That's a five-sided die. rand(5) gives a number between 0 and 4
    > inclusive.
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >



    --
    regards,
    vadivelan
    vadivelan K, Sep 7, 2010
    #7
  8. Hd Pwnz0r

    Josh Cheek Guest

    [Note: parts of this message were removed to make it a legal post.]

    On Tue, Sep 7, 2010 at 4:03 AM, vadivelan K <> wrote:

    > Thanks Brian for pointed out this. Yes it will returns the value bw 0 and 4
    > suits for 5-sided die.
    >
    > We can use this
    >
    > puts "Roll #{n+1}: #{1 + rand(7-1)}"
    >
    > This way we can get the values between 0 to 6 as final result.
    >
    >
    >


    You will get values 1 through 6 (Isn't that why you added the 1?) Also,
    seven minus one is six, so why the superfluous math?

    (0..1000).map { 1 + rand(6) }.uniq.sort # => [1, 2, 3, 4, 5, 6]
    Josh Cheek, Sep 7, 2010
    #8
    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. Ruby Quiz

    [QUIZ] Dice Roller (#61)

    Ruby Quiz, Jan 6, 2006, in forum: Ruby
    Replies:
    106
    Views:
    854
    Morus Walter
    Jan 10, 2006
  2. Paul Novak
    Replies:
    2
    Views:
    126
    Joby Bednar
    Jan 10, 2006
  3. John Earles

    [SOLUTION] Dice Roller (#61)

    John Earles, Jan 8, 2006, in forum: Ruby
    Replies:
    0
    Views:
    145
    John Earles
    Jan 8, 2006
  4. Stefan Walk

    [SOLUTION] Dice Roller

    Stefan Walk, Jan 8, 2006, in forum: Ruby
    Replies:
    0
    Views:
    85
    Stefan Walk
    Jan 8, 2006
  5. Matthew Moss

    [QUIZ.SUMMARY] Dice Roller (#61)

    Matthew Moss, Jan 12, 2006, in forum: Ruby
    Replies:
    11
    Views:
    196
    J. Ryan Sobol
    Jan 13, 2006
Loading...

Share This Page