Home » Ruby programming » Ruby programs

Ruby program to generate random numbers

Generating random numbers: Here, we are going to learn how to generate random numbers in Ruby programming language?
Submitted by Hrithik Chandra Prasad, on August 10, 2019

Generating random number

The task is to generate and print random number.

Generating random numbers means that any number can be provided to you which is not dependent on any pre-specified condition. It can be anything but must be within a range or limit. Ruby provides you the method for meeting the purpose.

Methods used:

  • puts: This method is used to put strings as messages on the screen for creating a better interaction with the user.
  • gets: This method is used to take input from the user.
  • rand: This method is a pre-defined method in Ruby library which Is specifically defined for generating a random number. It can be invoked with parameters only otherwise it will give decimal results which are most of the times less than 0. The examples are:

Variables used:

  • up: It is used to store the upper limit.
  • lm: It is used to store the lower limit.

Ruby code to generate random numbers

Ruby program to pick a random number from a range

#input upper and lower limits
puts "Enter upper limit"
puts "Enter lower limit"

#generate and print the random numbers
#between the given lower and upper limit
puts "The random numbers are..."
puts rand(lm..up)
puts rand(lm..up)
puts rand(lm..up)
puts rand(lm..up)
puts rand(lm..up)


Enter upper limit
Enter lower limit
The random numbers are...

Additional program:

The same concept can be applied to create a lucky draw program in which user will enter his/her name and they will get to know what they have won based on the random number generated by the program.

Ruby program for Lucky draw.

puts "Lucky Draw"
#input the name
puts "Enter your name"

#generate a random number 
#pick a lucky number
chk=rand(8) #For getting a random value

#print the result based on the random
#generated lucky number
case chk
when 0
	puts "#{name} got Maruti 800"
when 3
	puts "#{name} won iphone X"
when 8
	puts "#{name} won Rs 10"
when 6
	puts "#{name} won Samsung A50"
	puts "#{name}, Better luck next time"


RUN 1 : 
Lucky Draw
Enter your name
Sunaina, Better luck next time

RUN 2: 
Lucky Draw
Enter your name
Hargun, Better luck next time

RUN 3 :
Lucky Draw
Enter your name
Kajal won iphone X

RUN 4: 
Lucky Draw
Enter your name
Shivang got Maruti 800

Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.