Skip to content
EspañolWorkspace →

Random numbers in PSeInt: examples with Azar

13 examples

How it works

Azar(n) returns a random whole number from 0 to n - 1. For a die you write Azar(6) + 1, which gives 1 to 6. Aleatorio(a, b) gives a whole number between a and b directly, both included.

Random numbers make games (guess the number, rock paper scissors, rolling dice) and also test data: filling an array with random values to try out a sorting algorithm.

Every run gives different results, so when checking a program it helps to print the number that came up.

Short example

The least you need to see it work. Copy it or open it in the editor.

random-numbers.psc · 6 lines
Algoritmo RollDice    Definir d1, d2 Como Entero    d1 <- Azar(6) + 1    d2 <- Aleatorio(1, 6)    Escribir "Rolled ", d1, " and ", d2, ": total ", d1 + d2FinAlgoritmo

What it prints:

Rolled 5 and 1: total 6
Open it in Codoritmo

Opens in the browser editor, nothing to install.

Solved exercises

Each exercise comes with the full code, its flowchart and a sample run.

  • Guess the Number

    Guess a secret number from 1 to 100 in seven tries.

    Intermediate
  • Bubble Sort

    Sorts random numbers by swapping neighbors, and stops as soon as they're in order.

    Intermediate
  • Selection Sort

    Finds the smallest of the remaining numbers and puts it in its place.

    Intermediate
  • Insertion Sort

    Inserts each number among the sorted ones, like arranging cards in your hand.

    Intermediate
  • Counting Sort

    Counts how many times each value appears, without comparing numbers.

    Intermediate
  • Binary Search

    Finds a value in a sorted array by halving the range every step.

    Intermediate
  • Linear vs. Binary Search

    Counts the comparisons each search needs on the same sorted list.

    Intermediate
  • Weekly Sales

    A table of sales by branch and day, with totals.

    Advanced
  • Shell Sort

    Insertion sort between far-apart elements, with a gap that shrinks every pass.

    Advanced
  • Merge Sort

    Splits the array in halves, sorts each one recursively and merges them.

    Advanced
  • Quicksort

    Picks a pivot, splits smaller from larger values and sorts each side recursively.

    Advanced
  • Heap Sort

    Arranges the array as a heap, then takes the largest out again and again.

    Advanced
  • Hangman

    Guess a secret word letter by letter, with six mistakes allowed.

    Advanced

In the language reference

The instructions involved, explained one by one.

Other concepts

Codoritmo is an independent, open-source project inspired by PSeInt and compatible with its syntax. It is not affiliated with the PSeInt project. Official PSeInt website.