# How to generate a random string in Ruby

``````(0...8).map { (65 + rand(26)).chr }.join
``````

I spend too much time golfing.

``````(0...50).map { ('a'..'z').to_a[rand(26)] }.join
``````

And a last one that’s even more confusing, but more flexible and wastes fewer cycles:

``````o = [('a'..'z'), ('A'..'Z')].map(&:to_a).flatten
string = (0...50).map { o[rand(o.length)] }.join
``````

If you want to generate some random text then use the following:

``````50.times.map { (0...(rand(10))).map { ('a'..'z').to_a[rand(26)] }.join }.join(" ")
``````

this code generates 50 random word string with words length less than 10 characters and then join with space
{"ft_lang_label":"__label__en","ft_lang_prob":0.59914535,"math_prob":0.9838477,"size":560,"snap":"2023-14-2023-23","text_gpt3_token_len":170,"char_repetition_ratio":0.1438849,"word_repetition_ratio":0.0,"special_character_ratio":0.4017857,"punctuation_ratio":0.2986111,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98380464,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-04-01T04:54:23Z\",\"WARC-Record-ID\":\"<urn:uuid:3313a412-c4bd-4ec9-a1eb-168ce46755fc>\",\"Content-Length\":\"54804\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:2468a929-bbb6-4901-b34b-83ad5690c208>\",\"WARC-Concurrent-To\":\"<urn:uuid:6936e1cb-7cad-4d87-82b2-19fe57b88fac>\",\"WARC-IP-Address\":\"89.117.139.172\",\"WARC-Target-URI\":\"https://tarikbilla.com/how-to-generate-a-random-string-in-ruby/\",\"WARC-Payload-Digest\":\"sha1:WZNFOXJHKQVDXXT6YBUB5KT3BZACE4YG\",\"WARC-Block-Digest\":\"sha1:YGS6CU5FVQWF55FOYNVLZILNHHPPJSD6\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-14/CC-MAIN-2023-14_segments_1679296949701.0_warc_CC-MAIN-20230401032604-20230401062604-00679.warc.gz\"}"}