乱数とシード
NumPyで再現可能な合成データを作成します
「乱数とシード」はCoddyKit上の無料Data Science Academyレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはData Science Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Data Science Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Why Generate Random Data
Real datasets are not always handy. Random numbers let you build test data, simulate scenarios, and shuffle samples on demand.
The Modern Generator
Start by creating a generator with default_rng. This is the current, recommended way to make random numbers in NumPy.
rng = np.random.default_rng()Uniform Floats
Call random on the generator for floats between 0 and 1. Pass a size to get a whole array at once.
rng.random(3) # 3 floats in [0, 1)Random Integers
Use integers to draw whole numbers in a range. The low bound is included and the high bound is excluded.
rng.integers(1, 7, size=5) # like dice rollsDraw From a Normal Curve
The normal method samples a bell curve. Give it a mean and a standard deviation to shape the spread of values.
rng.normal(0, 1, size=4)The Problem With Randomness
Run random code twice and you get different numbers. That breaks reproducibility when a teammate cannot recreate your results.
Seeds Fix the Sequence
A seed sets the generator to a known start. Same seed means the exact same numbers, every single run.
rng = np.random.default_rng(42)Same Seed, Same Output
Two generators built with seed 42 produce identical arrays. That is how notebooks stay reproducible across machines.
a = np.random.default_rng(42).random(3)
b = np.random.default_rng(42).random(3) # a == bShuffle and Sample
Generators also shuffle arrays and pick random samples with choice, perfect for splitting data or drawing test cases.
rng.choice([10, 20, 30], size=2)Why It Matters for Science
Seeding makes experiments repeatable. Reviewers can rerun your code and confirm they see the very same outcome you reported.
Prefer the New API
You may still see the older np.random.seed style. New code should prefer the default_rng generator for cleaner, safer randomness.
Quick Check
A colleague reruns your notebook and gets different random results.
Randomness Recap
You generated floats, ints, and normal samples, then locked them with a seed so your results are reproducible anywhere. 🎲
よくある質問
「乱数とシード」レッスンは無料ですか?
はい。「乱数とシード」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Data Science Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Data Science Academyコースには全4レッスンが含まれています。
「乱数とシード」で何を学びますか?
NumPyで再現可能な合成データを作成します ブラウザで直接実行するハンズオンコードでData Science Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Data Science Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのData Science Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「乱数とシード」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このData Science Academyレッスンでコードを書いて実行できますか?
はい。すべてのData Science Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。