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.
Algoritmo RollDice Definir d1, d2 Como Entero d1 <- Azar(6) + 1 d2 <- Aleatorio(1, 6) Escribir "Rolled ", d1, " and ", d2, ": total ", d1 + d2FinAlgoritmoWhat it prints:
Rolled 5 and 1: total 6Opens 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.
IntermediateBubble Sort
Sorts random numbers by swapping neighbors, and stops as soon as they're in order.
IntermediateSelection Sort
Finds the smallest of the remaining numbers and puts it in its place.
IntermediateInsertion Sort
Inserts each number among the sorted ones, like arranging cards in your hand.
IntermediateCounting Sort
Counts how many times each value appears, without comparing numbers.
IntermediateBinary Search
Finds a value in a sorted array by halving the range every step.
IntermediateLinear vs. Binary Search
Counts the comparisons each search needs on the same sorted list.
IntermediateWeekly Sales
A table of sales by branch and day, with totals.
AdvancedShell Sort
Insertion sort between far-apart elements, with a gap that shrinks every pass.
AdvancedMerge Sort
Splits the array in halves, sorts each one recursively and merges them.
AdvancedQuicksort
Picks a pivot, splits smaller from larger values and sorts each side recursively.
AdvancedHeap Sort
Arranges the array as a heap, then takes the largest out again and again.
AdvancedHangman
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.