1. ランダムな数
1.1 ランダムな整数
Random(a, b)
は a
以上 b
以下の数を返します

# include <Siv3D.hpp>
void Main()
{
while (System::Update())
{
if (MouseL.down())
{
Print << Random(1, 6);
}
}
}
1.2 ランダムな浮動小数点数

# include <Siv3D.hpp>
void Main()
{
while (System::Update())
{
if (MouseL.down())
{
Print << U"{:.3f}"_fmt(Random(0.0, 1.0));
}
}
}
1.3 ランダムな場所
- 左クリックすると絵文字がランダムな場所に移動するサンプルです

# include <Siv3D.hpp>
void Main()
{
Scene::SetBackground(ColorF{ 0.8, 0.9, 1.0 });
Texture emoji{ U"☃️"_emoji };
// 絵文字の X 座標
int32 x = 400;
// 絵文字の Y 座標
int32 y = 300;
while (System::Update())
{
if (MouseL.down())
{
x = Random(0, 800);
y = Random(0, 600);
}
emoji.drawAt(x, y);
}
}
1.4 占い
- 左クリックするとランダムな占い結果が表示されるサンプルです

# include <Siv3D.hpp>
void Main()
{
Scene::SetBackground(ColorF{ 0.8, 0.9, 1.0 });
Font font{ FontMethod::MSDF, 48 };
int32 n = Random(0, 3);
while (System::Update())
{
if (MouseL.down())
{
n = Random(0, 3);
}
if (n == 0)
{
font(U"あなたの運勢は大吉です。").draw(40, 20, 20, Palette::Black);
}
else if (n == 1)
{
font(U"あなたの運勢は中吉です。").draw(40, 20, 20, Palette::Black);
}
else if (n == 2)
{
font(U"あなたの運勢は小吉です。").draw(40, 20, 20, Palette::Black);
}
else
{
font(U"あなたの運勢は凶です。").draw(40, 20, 20, Palette::Black);
}
}
}
1.5 ランダムな色
- 左クリックすると背景がランダムな色になるサンプルです

# include <Siv3D.hpp>
void Main()
{
double r = 0.8;
double g = 0.9;
double b = 1.0;
while (System::Update())
{
if (MouseL.down())
{
r = Random(0.0, 1.0);
g = Random(0.0, 1.0);
b = Random(0.0, 1.0);
}
Scene::SetBackground(ColorF{ r, g, b });
}
}