5.乱数の作成
(1) 動作
button1を押すと、label1に0から9までの整数がでたらめに表示される。
(2) Visual Basicの書き方
Private Sub button1_Click()
Dim nRnd As Integer
Randomize
nRnd = Int(10 * Rnd)
label1.Caption = nRnd
End Sub
(3) Visual J++の書き方
private void button1_click(Object source, Event e)
{
Random rndObj = new Random();
int nRnd = Math.abs(rndObj.nextInt() % 10);
label1.setText(String.valueOf(nRnd));
}
(4) Visual C++の書き方
void CProject1Dlg::OnButton1()
{
srand(time(NULL));
int nRnd = rand() % 10;
m_LABEL1.Format("%d", nRnd);
UpdateData(FALSE);
}