ruby 生成隨機字符串_Ruby程序生成隨機數

ruby 生成隨機字符串

產生隨機數 (Generating random number)

The task is to generate and print random number.

任務是生成并打印隨機數。

Generating random numbers means that any number can be provided to you which is not dependent on any pre-specified condition. It can be anything but must be within a range or limit. Ruby provides you the method for meeting the purpose.

生成隨機數意味著可以為您提供任何數字,而不依賴于任何預先指定的條件。 它可以是任何東西,但必須在范圍或限制之內。 Ruby為您提供了達到目的的方法。

Methods used:

使用的方法:

  • puts: This method is used to put strings as messages on the screen for creating a better interaction with the user.

    puts :此方法用于將字符串作為消息放在屏幕上,以與用戶建立更好的交互。

  • gets: This method is used to take input from the user.

    gets :此方法用于接收用戶的輸入。

  • rand: This method is a pre-defined method in Ruby library which Is specifically defined for generating a random number. It can be invoked with parameters only otherwise it will give decimal results which are most of the times less than 0. The examples are:

    rand :此方法是Ruby庫中的預定義方法,專門為生成隨機數而定義。 只能使用參數調用它,否則它將給出十進制結果,大多數情況下小于10。示例包括:

        rand(6)
    rand(0..6)
    rand(9..24)
    

Variables used:

使用的變量:

  • up: It is used to store the upper limit.

    up :用于存儲上限。

  • lm: It is used to store the lower limit.

    lm :用于存儲下限。

Ruby代碼生成隨機數 (Ruby code to generate random numbers)

=begin 
Ruby program to pick a random number from a range
=end
#input upper and lower limits
puts "Enter upper limit"
up=gets.chomp.to_i
puts "Enter lower limit"
lm=gets.chomp.to_i
#generate and print the random numbers
#between the given lower and upper limit
puts "The random numbers are..."
puts rand(lm..up)
puts rand(lm..up)
puts rand(lm..up)
puts rand(lm..up)
puts rand(lm..up)

Output

輸出量

Enter upper limit
100
Enter lower limit
50
The random numbers are...
91
98
96
95
84

Additional program:

附加程序:

The same concept can be applied to create a lucky draw program in which user will enter his/her name and they will get to know what they have won based on the random number generated by the program.

可以將相同的概念應用于創建幸運抽獎程序,在該程序中,用戶將輸入他/她的名字,并且他們將基于該程序生成的隨機數來知道自己贏了什么。

=begin 
Ruby program for Lucky draw.
=end
puts "Lucky Draw"
#input the name
puts "Enter your name"
name=gets.chomp
#generate a random number 
#pick a lucky number
chk=rand(8) #For getting a random value
#print the result based on the random
#generated lucky number
case chk
when 0
puts "#{name} got Maruti 800"
when 3
puts "#{name} won iphone X"
when 8
puts "#{name} won Rs 10"
when 6
puts "#{name} won Samsung A50"
else
puts "#{name}, Better luck next time"
end

Output

輸出量

RUN 1 : 
Lucky Draw
Enter your name
Sunaina
Sunaina, Better luck next time
RUN 2: 
Lucky Draw
Enter your name
Hargun
Hargun, Better luck next time
RUN 3 :
Lucky Draw
Enter your name
Kajal
Kajal won iphone X
RUN 4: 
Lucky Draw
Enter your name
Shivang
Shivang got Maruti 800

翻譯自: https://www.includehelp.com/ruby/generate-random-numbers.aspx

ruby 生成隨機字符串

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377654.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377654.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377654.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

leetcode 322. 零錢兌換 思考分析

目錄1、題目2、思路分析3、參考鏈接1、題目 給定不同面額的硬幣 coins 和一個總金額 amount。編寫一個函數來計算可以湊成總金額所需的最少的硬幣個數。如果沒有任何一種硬幣組合能組成總金額,返回 -1。 你可以認為每種硬幣的數量是無限的。 提示: 1 …

linux上的英文字體monospace可以在windows用嗎?

linux的字體都是開源的,應該可以官方下載本地下載轉載于:https://www.cnblogs.com/52linux/archive/2012/03/14/2396103.html

Flash Builder 創建CSS

1.global 選擇器將樣式應用于所有控件 在 Flash Builder 中創建新MXML 文件并切換到設計模式 屬性視圖右側的外觀視圖可更改外觀 Flash Builder 自動創建CSS 文件 CSS 文件有2 個命名空間: s 指 Spark 組件 mx 指 MX 組件 1. Global 與Application 選擇器 global …

ruby打印_Ruby程序打印數字的力量

ruby打印Ruby中數字的冪 (Power of a number in Ruby) The task to develop a program that prints power of a number in Ruby programming language. 開發可以用Ruby編程語言打印數字冪的程序的任務。 If we want to calculate the power of a number manually then we have…

二、訓練fashion_mnist數據集

一、加載fashion_mnist數據集 fashion_mnist數據集中數據為28*28大小的10分類衣物數據集 其中訓練集60000張,測試集10000張 from tensorflow import keras import tensorflow as tf import matplotlib.pyplot as plt import numpy as npfashion_mnist keras.data…

jquerymobile 切換頁面時候閃爍問題

https://github.com/jquery/jquery-mobile/commit/acbec71e29b6acec6cd2087e84e8434fecc0053f 可以修改css好像是個bug -4,9 4,10 * Dual licensed under the MIT (MIT-LICENSE.txt) or GPL (GPL-LICENSE.txt) licenses.*/.spin {--webkit-animation-name: spin;--webkit-an…

二分法:兩個有序數組長度為N,找到第N、N+1大的數

題目 兩個有序數組長度為N,找到第N、N1大的數 思路1:雙指針,O(N)復雜度 簡述思路: 如果當前A指針指向的數組A的內容小于B指針指向的數組B的內容,那么A指針往右移動,然后nums(當前已經遍歷過的數字個數)也…

Javascript -- In

http://www.caveofprogramming.com/articles/javascript-2/javascript-in-using-the-in-operator-to-iterate-through-arrays-and-objects/ http://msdn.microsoft.com/en-us/library/ie/9k25hbz2(vvs.94).aspx轉載于:https://www.cnblogs.com/daishuguang/p/3392310.html

三、自動終止訓練

有時候,當模型損失函數值預期的效果時,就可以結束訓練了,一方面節約時間,另一方面防止過擬合 此時,設置損失函數值小于0.4,訓練停止 from tensorflow import keras import tensorflow as tf import matplo…

矩陣形狀| 使用Python的線性代數

Prerequisite: Linear Algebra | Defining a Matrix 先決條件: 線性代數| 定義矩陣 In the python code, we will add two Matrices. We can add two Matrices only and only if both the matrices have the same dimensions. Therefore, knowing the dimensions o…

[數據庫]oracle客戶端連服務器錯誤

昨天晚上和今天上午用11g客戶端連同事10g服務器,報錯: The Network Adapter could not establish the connection 檢查嘗試了好多次都沒好。 用程序連,依舊是報這個錯,所以一查就解決了! 參考:http://apps…

ASP.NET 抓取網頁內容

(轉)ASP.NET 抓取網頁內容 ASP.NET 抓取網頁內容-文字 ASP.NET 中抓取網頁內容是非常方便的,而其中更是解決了 ASP 中困擾我們的編碼問題。 需要三個類:WebRequest、WebResponse、StreamReader。 WebRequest、WebRespo…

leetcode 53. 最大子序和 動態規劃解法、貪心法以及二分法

題目 給定一個整數數組 nums ,找到一個具有最大和的連續子數組(子數組最少包含一個元素),返回其最大和。 示例: 輸入: [-2,1,-3,4,-1,2,1,-5,4] 輸出: 6 解釋: 連續子數組 [4,-1,2,1] 的和最大,為 6。 進階: 如果你…

四、卷積神經網絡(Convolution Neural Networks)

一、CNN(Convolution Neural Networks) 卷積神經網絡基本思想:識別物體的特征,來進行判斷物體 卷積Convolution:過濾器filter中的數值與圖片像素值對應相乘再相加,6 * 6卷積一次(步數為1)變成4 * 4 Max Pooling:對卷積…

POJ3096Surprising Strings(map)

題意:輸入很多字符串,以星號結束。判斷每個字符串是不是“Surprising Strings”,判斷方法是:以“ZGBG”為例,“0-pairs”是ZG,GB,BG,這三個子串不相同,所以是“0-unique”…

vs助手使用期過 編譯CEGUI的問題:error C2061: 語法錯誤: 標識符“__RPC__out_xcount_part” VS2010...

第一個問題,下一個破解版的VX_A.dll,將其覆蓋以前的dll即可, 但是目錄有所要求,如下: XP系統:系統盤\Documents and Settings\用戶名\Local Settings\Application win7或者vistaData\Microsoft\VisualStud…

五、項目實戰---識別人和馬

一、準備訓練數據 下載數據集 validation驗證集 train訓練集 數據集結構如下: 將數據集解壓到自己選擇的目錄下就行 最后的結構效果如下: 二、構建模型 ImageDataGenerator 真實數據中,往往圖片尺寸大小不一,需要裁剪成一樣…

leetcode 122. 買賣股票的最佳時機 II 思考分析

目錄題目貪心法題目 給定一個數組,它的第 i 個元素是一支給定股票第 i 天的價格。 設計一個算法來計算你所能獲取的最大利潤。你可以盡可能地完成更多的交易(多次買賣一支股票)。 注意:你不能同時參與多筆交易(你必…

css設置a連接禁用樣式_使用CSS禁用鏈接

css設置a連接禁用樣式Question: 題: Links are one of the most essential aspects of any web page or website. They play a very important role in making our website or web page quite responsive or interactive. So the topic for discussion is quite pe…

服務器出現 HTTP 錯誤代碼,及解決方法

HTTP 400 - 請求無效 HTTP 401.1 - 未授權:登錄失敗 HTTP 401.2 - 未授權:服務器配置問題導致登錄失敗 HTTP 401.3 - ACL 禁止訪問資源 HTTP 401.4 - 未授權:授權被篩選器拒絕 HTTP 401.5 - 未授權:ISAPI 或 CGI 授權失敗 HTTP 40…