TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to RUBY

Ruby Random Number Generator: rand, srand

Generate random numbers with rand. Use srand to seed a random stream.
Rand, random numbers. The weather is not random. It can be forecast based on patterns. But it can be unpredictable. A random number generator like rand could generate temperatures.
With rand, we often provide a range of numbers. And with this range, we can look up a value from an array—like a letter or an array element.
Range example. Here we use rand with a range. The range is inclusive—the lowest or highest bounding numbers may be returned by rand. By default, a range of 0 to 1 is used.

Here: We generate a random number between 0 and 1. Then we generate six more, all between 0 and 5 (inclusively).

Srand: The srand method is also available: this seeds the random number generator. Srand is not normally needed.

Note: Calling srand with a constant number will make a program always use the same random numbers.

Ruby program that uses random numbers # Display random number between 0 and 1. puts rand # Display 6 random numbers between 0 and 5 inclusive. for i in 0..5 # Call with range. r = rand 0..5 puts r end Output 0.3299959902490742 2 4 2 0 1 5
Srand. This seeds the random number generator. Typically it is best not to invoke this method. It may make the random number stream more deterministic.

Note: If you use a constant with srand, the random stream of numbers is not random anymore. This does not seem random.

Instead: Each time I run this program, it returns 0.41 and 0.72. This is because the random number generator was seeded with a constant 1.

Ruby program that uses srand # Use constant seed. srand 1 # Display random numbers. puts rand puts rand Output 0.417022004702574 0.7203244934421581
Lowercase letters. There are 26 lowercase ASCII letters in the English alphabet. With rand we can generate random lowercase letters with an index from 0 to 25 (inclusive).

Note: The "letters" string has 26 chars. Index 0 is "a" and index 25 is "z." Other values could be added.

Ruby program that generates random lowercase letters def random_lowercase() # Lookup table of lowercase letters. letters = "abcdefghijklmnopqrstuvwxyz" # Get random index within range. # ... Return letter at that position. r = rand 0..25 return letters[r] end # Invoke our random_lowercase method. 10.times do puts random_lowercase() end Output l a q i v s i b d q
Random strings. This method builds on our "random lowercase letter" method. We generate a string of a certain length. We place a random letter "length" times.

Array: We start with an empty array (we call this array the "word" array). We add letters to this array.

Times: We use the times iterator to add the required number of letters to our word array. We access letters using a range-based rand call.

Ruby program that generates random strings def random_string(length) # All lowercase letters. letters = "abcdefghijklmnopqrstuvwxyz" # Empty Array for letters we add. word = Array[] # Add all random letters. length.times do # Get random index within string. r = rand 0..25 # Get letter for index. letter = letters[r] # Push to our array. word.push(letter) end # Return random string joined together. result = word.join("") end # Call our random_string method many times. 3.upto(10) do |i| puts random_string(i) puts random_string(i) end Output oko pig heav yafh yvcuu wgbep uaosje uywvyc pttwiau wqcxpyq zzudlihd kckqhqfd tffatuubx oulqkdfyw civpxnsdjb yrvvqbdwsv
Randomized review. It may be that the core of our universe is random. With Ruby, though, we get a powerful and easy-to-use pseudo-random number generator.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf