[Ruby] 4-digit random number generation with sprintf

Purpose

I wanted to generate a PIN code, so I used the sprintf and rand methods.

code

sprintf('%04d', rand(10000)) //Fill the remaining part with 0 for 4 digits
=>"1184"

Other

sprintf('%10d', rand(10000)) //Right-handed with a width of 10
sprintf('%-10d', rand(10000)) //Left-handed with a width of 10
=> "      7179"

Recommended Posts

[Ruby] 4-digit random number generation with sprintf
java (random number)
C # (random number) memo
[Java] Random number generation method (Random)
[Ruby] 4-digit random number generation with sprintf
[Java] Random number generation method (Random)
java (random number)
I checked the number of taxis with Ruby
Install Ruby 3.0.0 with asdf
C # (random number) memo
[Ruby] About instance generation
11th, Classification with Ruby
Evolve Eevee with Ruby