Coffeescript-math-random

提供:Dev Guides
移動先:案内検索

CoffeeScript Math-random()

説明

  • random()*メソッドは、0(包括的)から1(排他的)の間の乱数を返します。

構文

以下に、JavaScriptの* random()*メソッドの構文を示します。 CoffeeScriptコードで同じメソッドを使用できます。

Math.random ( x )

次の例は、CoffeeScriptでの* random()メソッドの使用法を示しています。 このコードを *math_random.coffee という名前のファイルに保存します。

value = Math.random()
console.log "The first number is : " + value

value = Math.random()
console.log "The second number is : " + value

value = Math.random()
console.log "The third number is : " + value
  • コマンドプロンプト*を開き、以下に示すように.coffeeファイルをコンパイルします。
c:\> coffee -c math_random.coffee

コンパイル時に、次のJavaScriptが提供されます。

//Generated by CoffeeScript 1.10.0
(function() {
  var value;

  value = Math.random();

  console.log("The first number is : " + value);

  value = Math.random();

  console.log("The second number is : " + value);

  value = Math.random();

  console.log("The third number is : " + value);


}).call(this);

次に、*コマンドプロンプト*を再度開き、以下に示すようにCoffeeScriptファイルを実行します。

c:\> coffee math_tan.coffee

CoffeeScriptファイルを実行すると、次の出力が生成されます。

The first number is : 0.44820353598333895
The second number is : 0.10985115729272366
The third number is : 0.6831563576124609