Ruby Coding Exercise: How to Generate an Array of Random Numbers in Ruby

In this coding exercise I walk through a TDD process for implementing an Array generator that contains random numbers in a specific range.

Summary

Generate an array of 20 random integers, with the integer values ranging from 1 to 1000.

Exercise File

Code File

Exercise Description

Write a program that automatically generates an array containing 20 random integers, ranging from 1 to 1000.

Sample Output

=> [695, 627, 849, 315, 203, 520, 433, 837, 923, 796, 6, 472, 964, 717, 549, 963, 787, 20, 589, 628]

Real World Usage

Being able to generate a set of random numbers is common in a number of different scenarios, including:

  • Game development
  • Creating test data
  • Algorithm implementations

Solution

Can be found on the solutions branch on github.

Jordan Hudgens

view all post
Leave a comment

Please be polite. We appreciate that.

By Daniele Zedda • 18 February

← PREV POST

By Daniele Zedda • 18 February

NEXT POST → 34
Share on