E-Learning

Learn Ruby – How To Get a Random Number

 
Ruby has a creator of random numbers (or aleatory) already integrated into its code, which is the method rand. Since version 1.9.3, Ruby added the ability to use ranges as arguments to rand, which produces a more fluid code.

Now we are going to examine some of the ways to create a random number using the rand method and its variations.
 

Ruby – Creating random number using rand

If you call rand directly, you get a float greater than or equal to 0.0 and less than 1.0.

rand()
0.0047585340125684

If you put in it an integer argument (such as, for example, rand (7)), you will get an integer value greater than or equal to 0 and less than 7.

rand (7)
5

If you are looking for a number within a particular range you can use:

rand (1 ..10)
8

Where the initial value is the lower range limit, and the final value will include the higher limit.

If you are looking for a non-inclusive range, you can use 3 points:

rand (1 ...10)
9

This will delete the final value of the random number generation, in this case, 10. You could do the same thing using the previous form and entering the last value that you want to include:

rand (1 ..9)
8

 

Ruby – Creating random number using random

The random.rand works quite similar to rand:

Random.rand (10...42) # => Same as rand (10 ...42)

However, when the argument value is a negative integer or zero, this method returns ArgumentError, unlike rand.

Do you know other ways to create random numbers in Ruby? Share your comments in the section below!

If you want to explore other issues, can check our LCTV videos about Ruby. Below we have some examples:

 

You can also subscribe to some channels that broadcasts in Ruby, such as the following:

iamjbecker

 

 

carlosvigil

 

 

Another cool way to find out interesting things about Ruby is to access our project page!

Dr. Michael J. Garbade

I, Dr. Michael J. Garbade is the co-founder of the Education Ecosystem (aka LiveEdu), ex-Amazon, GE, Rebate Networks, Y-combinator. Python, Django, and DevOps Engineer. Serial Entrepreneur. Experienced in raising venture funding. I speak English and German as mother tongues. I have a Masters in Business Administration and Physics, and a Ph.D. in Venture Capital Financing. Currently, I am the Project Lead on the community project -Nationalcoronalvirus Hotline I write subject matter expert technical and business articles in leading blogs like Opensource.com, Dzone.com, Cybrary, Businessinsider, Entrepreneur.com, TechinAsia, Coindesk, and Cointelegraph. I am a frequent speaker and panelist at tech and blockchain conferences around the globe. I serve as a start-up mentor at Axel Springer Accelerator, NY Edtech Accelerator, Seedstars, and Learnlaunch Accelerator. I love hackathons and often serve as a technical judge on hackathon panels.

View Comments

Recent Posts

Blockchain in Elections: A Leap Toward Transparent Democracy

In 2024 we're witnessing a critical point in democratic technology: the integration of blockchain and…

3 weeks ago

Win Big with Our Amazon Fire Max 11 & AirPods Pro Giveaway!

We’re thrilled to announce an exciting opportunity for you to win not one but two…

2 months ago

Unleashing Potential: How Education Ecosystem Transforms Learning into Real-World Success

Acquiring practical skills is crucial for career advancement and personal growth. Education Ecosystem stands out…

4 months ago

The Role of Artificial Intelligence in Modern Software Development

Artificial Intelligence (AI) has been making significant strides in various industries, and the software development…

7 months ago

Highest Stable Coin Yields – (W16 – 2024)

Another week to bring you the top yield platforms for three of the most prominent…

8 months ago

LEDU Token OTC Trading

If you hold a large volume of LEDU tokens above 1 million units and wish…

9 months ago