Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,948 questions

51,890 answers

573 users

How to generate random number in Ruby

6 Answers

0 votes
r = Random.new
 
i = 0
         
while (i < 20) 
    number = r.rand(5..12) # 5 - 12
    puts number
    i += 1
end
 
 
 
#
# run:
# 
# 5
# 8
# 6
# 7
# 12
# 11
# 9
# 6
# 10
# 12
# 11
# 8
# 6
# 12
# 7
# 6
# 12
# 9
# 8
# 9
#

 



answered Oct 13, 2021 by avibootz
edited Oct 16, 2021 by avibootz
0 votes
r = Random.new
 
i = 0
         
while (i < 10) 
    number = rand() # 0.0 - 1.0
    puts number
    i += 1
end
 
 
 
#
# run:
# 
# 0.5708090998433002
# 0.008725630054524869
# 0.6255400586231457
# 0.4750306981651582
# 0.813154470580231
# 0.29935653831713416
# 0.3554266593922807
# 0.7604221459941487
# 0.4315453295944536
# 0.7462582554522872
#

 



answered Oct 13, 2021 by avibootz
edited Oct 16, 2021 by avibootz
0 votes
r = Random.new
 
i = 0
         
while (i < 10) 
    number = rand(7) # 0 - 6
    puts number
    i += 1
end
 
 
 
#
# run:
# 
# 1
# 2
# 4
# 1
# 5 
# 6
# 3
# 0
# 6
# 1
#

 



answered Oct 13, 2021 by avibootz
edited Oct 16, 2021 by avibootz
0 votes
r = Random.new
  
i = 0
          
while (i < 10) 
    number = r.rand(5...12) # 5 - 11
    puts number
    i += 1
end
 
 
 
#
# run:
# 
# 7
# 5
# 6
# 8
# 6
# 11
# 8
# 5
# 7
# 9
#

 



answered Oct 13, 2021 by avibootz
edited Oct 16, 2021 by avibootz
0 votes
r = Random.new
  
i = 0
          
while (i < 10) 
    number = r.rand(1.1..1.3) # 1.1 - 1.3
    puts number 
    i += 1
end
  
  
  
  
#
# run:
# 
# 1.171608391106416
# 1.1746373075796785
# 1.1055832997321542
# 1.1730243970999052
# 1.2116170405698443
# 1.2949532574481895
# 1.238850888347789
# 1.230830181080644
# 1.1695634741516092
# 1.1945698296868605
#

 



answered Oct 14, 2021 by avibootz
edited Oct 16, 2021 by avibootz
0 votes
r = Random.new
  
i = 0
          
while (i < 20) 
    number = rand(-5..-1) # -5 : -1
    puts number
    i += 1
end
  
  
  
  
#
# run:
# 
# -1
# -3
# -5
# -3
# -3
# -3
# -5
# -3
# -4
# -5
# -1
# -5
# -1
# -2
# -2
# -1
# -5
# -1
# -5
# -3
#

 



answered Oct 14, 2021 by avibootz
edited Oct 16, 2021 by avibootz

Related questions

1 answer 166 views
1 answer 204 views
1 answer 169 views
1 answer 201 views
1 answer 182 views
1 answer 242 views
...