Skip to content

Instantly share code, notes, and snippets.

@CodePint
Created December 11, 2017 21:12
Show Gist options
  • Save CodePint/943b5e2c254bec057128b77e924b0090 to your computer and use it in GitHub Desktop.
Save CodePint/943b5e2c254bec057128b77e924b0090 to your computer and use it in GitHub Desktop.
##random number
=begin
Goal
By using the random module, python can do things like pseudo-random number generation.
So in this program, allow the user to input the amount of sides on a dice and how many times it should be rolled.
From there, your program should simulate dice rolls and keep track of how many times each number comes up (this does not have to be displayed).
After that, print out how many times each number came up.
Subgoal
Adjust your program so that if the user does not type in a number when they need to,
the program will keep prompting them to type in a real number until they do so.
Put the program into a loop so that the user can continue to simulate dice rolls without having to restart the entire program.
In addition to printing out how many times each side appeared, also print out the percentage it appeared. If you can,
round the percentage to 4 digits total OR two decimal places.
=end
def roll_dice(y, z)
x = 1
results_array = Array.new
z.times do rand_gen = rand(x..y)
results_array.push(rand_gen)
end
return results_array
end
def ask_loop
loop do
puts "lets roll some dice..."
puts "\n"
puts "how many dice do you want to roll? "
dice_roll = gets.chomp.to_i
puts "\n"
puts "how many sides are on the dice? "
dice_sides = gets.chomp.to_i
puts "you rolled a #{dice_sides} sided dice, #{dice_roll} times"
puts "\n"
puts "here are the results... "
print roll_dice(dice_sides, dice_roll)
puts "\n"
puts "would you like to roll the dice again?"
puts "\n"
puts "any key to continue, (n) to end the program"
roll_again = gets.chomp
if roll_again != "n"
puts "okay lets roll the dice again..."
ask_loop
else
puts "Thanks for rolling the dice..."
break
end
end
end
ask_loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment