# 配列

### [20170818] 10~16までのスクリプト実行ごとに変わるランダムな配列

a.rb
```h_concentration =  []

for num in 0...10 do
h_concentration.push((rand(10000..16000)/1000.0).round(1))
end

p h_concentration
```
```\$ ruby a.rb
[12.4, 14.4, 12.0, 15.8, 10.9, 14.9, 11.2, 16.0, 10.0, 13.2]
\$ ruby a.rb
[14.2, 14.1, 14.2, 13.1, 13.7, 10.1, 11.8, 15.3, 10.9, 10.6] 結果が変わる
```

### [20170818] 10~16までのスクリプト実行ごとに変わらないランダムな配列

b.rb
```  1 h_concentration =  []
2 init_seed = 12345
3 b = 0
4 total = 10
5
6 for num in 0...total do
7   prng1 = Random.new( num == 0? init_seed : b )
8   prng1.seed
9   b = prng1.rand(10000..16000)
10   h_concentration.push((b/1000.0).round(1))
11 end
12
13 p h_concentration
```
```\$ ruby bb.rb
[14.6, 14.2, 15.1, 13.3, 13.9, 14.4, 14.2, 14.1, 11.8, 15.2]
\$ ruby bb.rb
[14.6, 14.2, 15.1, 13.3, 13.9, 14.4, 14.2, 14.1, 11.8, 15.2]　結果が変わらない
```

bb.rb
```  1 init_seed = 12345
2
3 prng1=Random.new(init_seed)
4 #prng1.seed
5 a = Array.new(10){(prng1.rand(10.0...16.0)).round(1)} "これで10と16の確率が半分になるというのは問題ない？"
6 p a
```
```\$ ruby bb.rb
[15.6, 11.9, 11.1, 11.2, 13.4, 13.6, 15.8, 13.9, 14.5, 13.9]
\$ ruby bb.rb
[15.6, 11.9, 11.1, 11.2, 13.4, 13.6, 15.8, 13.9, 14.5, 13.9] 実行ごとにランダム配列の結果が変わらない
```

### [20170818] 等差数列

c.rb
```init_value = 9.95
class_interval_width=0.9
max_value = 16.00

i = ((max_value - init_value)/class_interval_width).ceil
class_interval = []

for num in 0...i do
class_interval.push( (init_value + class_interval_width*num).round(2) )
end

p class_interval
```
```\$ ruby c.rb
[9.95, 10.85, 11.75, 12.65, 13.55, 14.45, 15.35]
```

c.rbと同じことを簡単に実現する。

cc.rb
```init_value = 9.95
class_interval_width=0.9
max_value = 16.00

class_interval = init_value.step(max_value, class_interval_width).map{|i| i.round(2)}

p class_interval

```
```ruby cc.rb
[9.95, 10.85, 11.75, 12.65, 13.55, 14.45, 15.35]
```

# [20170818] 配列内の最小要素の位置を取得

```irb(main):020:0> a=Array.new(10){rand(0..100)}
=> [72, 84, 98, 14, 50, 81, 18, 36, 16, 67]
irb(main):021:0> a.each_with_index.map{|v, i| v == a.min ? i : nil}.compact
=> [3]
```

# [20170822] find_index

```irb(main):024:0> a
=> [10.0, 10.9, 11.8, 12.7, 13.6, 14.5, 15.4]
irb(main):026:0> a.find_index{|i| i >= 11}.to_i
=> 2
```

# [20170822] do

d.rb
```a = (1..10).map do |x|
x*3
end

p a
```
```\$ ruby d.rb
1
2
3
4
5
6
7
8
9
10
[3, 6, 9, 12, 15, 18, 21, 24, 27, 30]
```

# [20180629]

```irb(main):001:0> str="abcdefghi"
=> "abcdefghi"
irb(main):002:0> str.split(//,3)
=> ["a", "b", "cdefghi"]
irb(main):003:0> str.scan(/.{3}/)
=> ["abc", "def", "ghi"]
irb(main):004:0> str.match(/.{3}/)
=> #<MatchData "abc">
p "abcde-fghijklmno-pqrstuv,wxyz".delete("f-u,-")
=> "abcdevwxyz"
irb(main):020:0> str = "Ruby"
=> "Ruby"
irb(main):021:0> str[0..-2].swapcase
=> "rUB"
```