るりまサーチ

最速Rubyリファレンスマニュアル検索!
132件ヒット [1-100件を表示] (0.074秒)

別のキーワード

  1. _builtin end
  2. ripper end_seen?
  3. _builtin exclude_end?
  4. _builtin end_with?
  5. zlib end

ライブラリ

クラス

モジュール

キーワード

検索結果

<< 1 2 > >>

Random#rand -> Float (21302.0)

一様な擬似乱数を発生させます。

...は乱数の範囲から除かれます。
range.end - range.begin が整数を返す場合は range.begin + self.rand((range.end - range.begin) + e)
の値を返します(e は終端を含む場合は1、含まない場合は0です)。
range.end - range.begin が実数を返す場合も同様で...
...を Range オブジェクトで指定します。
range.end - range.begin は数値である必要があり、
range.begin + 数値 が適切な値を返す必要があります。

@raise Errno::EDOM rand(1..Float::INFINITY) などのように範囲に問題があるとき...
...ruby]{
# Kernel.#rand とほぼ同様の使い勝手
prng = Random.new(1234)
prng.rand # => 0.1915194503788923
srand(1234)
rand
# => 0.1915194503788923

# max に実数も指定出来る
prng.rand(6.5) # => 4.043707011758907
# (rand(6) と同等)
rand
(6.5)...

Random#rand(max) -> Integer | Float (21302.0)

一様な擬似乱数を発生させます。

...は乱数の範囲から除かれます。
range.end - range.begin が整数を返す場合は range.begin + self.rand((range.end - range.begin) + e)
の値を返します(e は終端を含む場合は1、含まない場合は0です)。
range.end - range.begin が実数を返す場合も同様で...
...を Range オブジェクトで指定します。
range.end - range.begin は数値である必要があり、
range.begin + 数値 が適切な値を返す必要があります。

@raise Errno::EDOM rand(1..Float::INFINITY) などのように範囲に問題があるとき...
...ruby]{
# Kernel.#rand とほぼ同様の使い勝手
prng = Random.new(1234)
prng.rand # => 0.1915194503788923
srand(1234)
rand
# => 0.1915194503788923

# max に実数も指定出来る
prng.rand(6.5) # => 4.043707011758907
# (rand(6) と同等)
rand
(6.5)...

Random#rand(range) -> Integer | Float (21302.0)

一様な擬似乱数を発生させます。

...は乱数の範囲から除かれます。
range.end - range.begin が整数を返す場合は range.begin + self.rand((range.end - range.begin) + e)
の値を返します(e は終端を含む場合は1、含まない場合は0です)。
range.end - range.begin が実数を返す場合も同様で...
...を Range オブジェクトで指定します。
range.end - range.begin は数値である必要があり、
range.begin + 数値 が適切な値を返す必要があります。

@raise Errno::EDOM rand(1..Float::INFINITY) などのように範囲に問題があるとき...
...ruby]{
# Kernel.#rand とほぼ同様の使い勝手
prng = Random.new(1234)
prng.rand # => 0.1915194503788923
srand(1234)
rand
# => 0.1915194503788923

# max に実数も指定出来る
prng.rand(6.5) # => 4.043707011758907
# (rand(6) と同等)
rand
(6.5)...

Enumerable#sort_by -> Enumerator (31.0)

ブロックの評価結果を <=> メソッドで比較することで、self を昇 順にソートします。ソートされた配列を新たに生成して返します。

...

//emlist[例][ruby]{
class Array
def sort_by
self.map {|i| [yield(i), i] }.
sort {|a, b| a[0] <=> b[0] }.
map {|i| i[1]}
end

end

//}

Enumerable#sort と比較して sort_by が優れている点として、
比較条件が複雑な場合の速度が挙げられます。
s...
...回数の検証結果を参照してみてください。

//emlist[][ruby]{
class Integer
def count
$n += 1
self
end

end


ary = []
1.upto(1000) {|v| ary << rand(v) }

$n = 0
ary.sort {|a,b| a.count <=> b.count }
p $n # => 18200

$n = 0
ary.sort_by {|v| v.count }
p $n #...

Enumerable#sort_by {|item| ... } -> [object] (31.0)

ブロックの評価結果を <=> メソッドで比較することで、self を昇 順にソートします。ソートされた配列を新たに生成して返します。

...

//emlist[例][ruby]{
class Array
def sort_by
self.map {|i| [yield(i), i] }.
sort {|a, b| a[0] <=> b[0] }.
map {|i| i[1]}
end

end

//}

Enumerable#sort と比較して sort_by が優れている点として、
比較条件が複雑な場合の速度が挙げられます。
s...
...回数の検証結果を参照してみてください。

//emlist[][ruby]{
class Integer
def count
$n += 1
self
end

end


ary = []
1.upto(1000) {|v| ary << rand(v) }

$n = 0
ary.sort {|a,b| a.count <=> b.count }
p $n # => 18200

$n = 0
ary.sort_by {|v| v.count }
p $n #...

絞り込み条件を変える

Enumerable#max_by -> Enumerator (19.0)

各要素を順番にブロックに渡して実行し、 その評価結果を <=> で比較して、 最大であった値に対応する元の要素、もしくは最大の n 要素が降順で入った配列を返します。

...merable
# weighted random sampling.
#
# Pavlos S. Efraimidis, Paul G. Spirakis
# Weighted random sampling with a reservoir
# Information Processing Letters
# Volume 97, Issue 5 (16 March 2006)
def wsample(n)
self.max_by(n) {|v| rand ** (1.0/yield(v)) }
end

end

e = (-20..20).to_a*...

Enumerable#max_by {|item| ... } -> object | nil (19.0)

各要素を順番にブロックに渡して実行し、 その評価結果を <=> で比較して、 最大であった値に対応する元の要素、もしくは最大の n 要素が降順で入った配列を返します。

...merable
# weighted random sampling.
#
# Pavlos S. Efraimidis, Paul G. Spirakis
# Weighted random sampling with a reservoir
# Information Processing Letters
# Volume 97, Issue 5 (16 March 2006)
def wsample(n)
self.max_by(n) {|v| rand ** (1.0/yield(v)) }
end

end

e = (-20..20).to_a*...

Enumerable#max_by(n) -> Enumerator (19.0)

各要素を順番にブロックに渡して実行し、 その評価結果を <=> で比較して、 最大であった値に対応する元の要素、もしくは最大の n 要素が降順で入った配列を返します。

...merable
# weighted random sampling.
#
# Pavlos S. Efraimidis, Paul G. Spirakis
# Weighted random sampling with a reservoir
# Information Processing Letters
# Volume 97, Issue 5 (16 March 2006)
def wsample(n)
self.max_by(n) {|v| rand ** (1.0/yield(v)) }
end

end

e = (-20..20).to_a*...

Enumerable#max_by(n) {|item| ... } -> Array (19.0)

各要素を順番にブロックに渡して実行し、 その評価結果を <=> で比較して、 最大であった値に対応する元の要素、もしくは最大の n 要素が降順で入った配列を返します。

...merable
# weighted random sampling.
#
# Pavlos S. Efraimidis, Paul G. Spirakis
# Weighted random sampling with a reservoir
# Information Processing Letters
# Volume 97, Issue 5 (16 March 2006)
def wsample(n)
self.max_by(n) {|v| rand ** (1.0/yield(v)) }
end

end

e = (-20..20).to_a*...
<< 1 2 > >>