def randth(arr)
randarr=Array.new
tmp=Array.new
s=arr.size-1
p s
for i in 0..s
r=rand(s+1)
if tmp[r]!=1
randarr.push(arr[r])
tmp[r]=1
else
redo
end
i=i+1
end
return randarr
end
p randth([1,2,3,4,5,6,7,8,9,10])
二:
def double(x) { x * 2 }
a_list = [1, 2, 3, 4]
map(a_list, double) # [2, 4, 6, 8]
san:
tree = a {
b { d e }
c { f g h }
}
三:
string randStr = " ";
Random randObj = new Random();
char[] codearray = { '1 ', '2 ', '3 ', '4 ', '5 ', '6 ', '7 ', '8 ', '9 ', '0 ', 'a ', 'b ', 'c ', 'd ', 'e ', 'f ', 'g ', 'h ', 'i ', 'j ', 'k ', 'l ', 'm ', 'n ', 'o ', 'p ', 'q ', 'r ', 's ', 't ', 'u ', 'v ', 'w ', 'x ', 'y ', 'z ' };
for (int i = 0; i < 6; i++)
{
randStr +=codearray[randObj.Next(codearray.Length)];
}
四:
[代码] hello_controller.rb
view sourceprint?
class HelloController < ApplicationController
def show
@images = ["image1.jpg", "image2.jpg", "image3.jpg", "image4.jpg", "image5.jpg"]
@random_no = rand(5)
@random_image = @images[@random_no]
end
end
[代码] show.rhtml
<html>
<head>
<title>Random Image</title>
</head>
<body>
<h1>Random Image</h1>
Displaying image <%= @random_image%>
<img src="/public/images/<%= @random_image%>">
</body>
</html>
randarr=Array.new
tmp=Array.new
s=arr.size-1
p s
for i in 0..s
r=rand(s+1)
if tmp[r]!=1
randarr.push(arr[r])
tmp[r]=1
else
redo
end
i=i+1
end
return randarr
end
p randth([1,2,3,4,5,6,7,8,9,10])
二:
def double(x) { x * 2 }
a_list = [1, 2, 3, 4]
map(a_list, double) # [2, 4, 6, 8]
san:
tree = a {
b { d e }
c { f g h }
}
三:
string randStr = " ";
Random randObj = new Random();
char[] codearray = { '1 ', '2 ', '3 ', '4 ', '5 ', '6 ', '7 ', '8 ', '9 ', '0 ', 'a ', 'b ', 'c ', 'd ', 'e ', 'f ', 'g ', 'h ', 'i ', 'j ', 'k ', 'l ', 'm ', 'n ', 'o ', 'p ', 'q ', 'r ', 's ', 't ', 'u ', 'v ', 'w ', 'x ', 'y ', 'z ' };
for (int i = 0; i < 6; i++)
{
randStr +=codearray[randObj.Next(codearray.Length)];
}
四:
[代码] hello_controller.rb
view sourceprint?
class HelloController < ApplicationController
def show
@images = ["image1.jpg", "image2.jpg", "image3.jpg", "image4.jpg", "image5.jpg"]
@random_no = rand(5)
@random_image = @images[@random_no]
end
end
[代码] show.rhtml
<html>
<head>
<title>Random Image</title>
</head>
<body>
<h1>Random Image</h1>
Displaying image <%= @random_image%>
<img src="/public/images/<%= @random_image%>">
</body>
</html>