0Pricing
Data Science Academy · Lezione

Numeri casuali e seed

Generi dati sintetici riproducibili con NumPy.

Numeri casuali e seed è una lezione Data Science Academy gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Data Science Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Data Science Academy include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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 rolls

Draw 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 == b

Shuffle 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. 🎲

Domande Frequenti

La lezione «Numeri casuali e seed» è gratuita?

Sì — il testo completo di «Numeri casuali e seed» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Data Science Academy, passa a CoddyKit PRO. Il corso Data Science Academy include 4 lezioni in totale.

Cosa imparerò in «Numeri casuali e seed»?

Generi dati sintetici riproducibili con NumPy. Eserciti Data Science Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Data Science Academy?

Non è richiesta alcuna esperienza precedente. Data Science Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.

Quanto tempo richiede la lezione «Numeri casuali e seed»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Data Science Academy?

Sì. Ogni lezione Data Science Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Ridimensionare e appiattire gli array
  2. Somma, media e il trucco degli assi
  3. Maschere booleane per la selezione
  4. Numeri casuali e seed
← Torna a Data Science Academy