Re: 100 Random Single Variable Linear Equations

Liste des GroupesRevenir à cl awk 
Sujet : Re: 100 Random Single Variable Linear Equations
De : porkchop (at) *nospam* invalid.foo (Mike Sanders)
Groupes : comp.lang.awk
Date : 06. Dec 2024, 05:51:54
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vitvta$24sm3$1@dont-email.me>
References : 1
User-Agent : tin/2.6.2-20221225 ("Pittyvaich") (NetBSD/9.3 (amd64))
Mike Sanders <porkchop@invalid.foo> wrote:

# algebra.awk: 2024 - Michael Sanders
#
# usage: awk -f algebra.awk > solve.txt
>
[...]

# subtle tweak: every equation unique (no duplicates)...

BEGIN {
    srand() # seed the random number generator

    # keep generating until we have exactly 100 unique equations
    while (u < 100) {
        a = int(rand() * 20) + 1 # random value for 'a' (1 to 20)
        b = int(rand() * 20) + 1 # random value for 'b' (1 to 20)
        c = int(rand() * 50) + 1 # random value for 'c' (1 to 50)

        opc = (rand() < 0.5 ? "*" : "/")      # random operator
        lhs = sprintf("%dx %s %d", a, opc, b) # left-hand side
        rhs = c                               # right-hand side
        equ = lhs " = " rhs                   # full equation

        # store equation in array if it doesn't already exist
        if (!(equ in equations)) {
            equations[equ] = 1 # mark element as 'reserved'...
            u++                # increment u for each unique equation
        }
    }

    # print equations
    for (e in equations) printf("%03d. %s\n\n\n\n\n\n\n\n\n", ++q, e)
}

# eof

--
:wq
Mike Sanders


Date Sujet#  Auteur
6 Dec 24 * 100 Random Single Variable Linear Equations23Mike Sanders
6 Dec 24 +* Re: 100 Random Single Variable Linear Equations19Mike Sanders
6 Dec 24 i`* Re: 100 Random Single Variable Linear Equations18Janis Papanagnou
6 Dec 24 i +* Re: 100 Random Single Variable Linear Equations3Janis Papanagnou
7 Dec 24 i i+- Re: 100 Random Single Variable Linear Equations1Mike Sanders
7 Dec 24 i i`- Re: 100 Random Single Variable Linear Equations1Mike Sanders
6 Dec 24 i `* Re: 100 Random Single Variable Linear Equations14Mike Sanders
6 Dec 24 i  +* Re: 100 Random Single Variable Linear Equations3Janis Papanagnou
7 Dec 24 i  i`* Re: 100 Random Single Variable Linear Equations2Christian Weisgerber
7 Dec 24 i  i `- Re: 100 Random Single Variable Linear Equations1Mike Sanders
6 Dec 24 i  +* Re: 100 Random Single Variable Linear Equations3Keith Thompson
7 Dec 24 i  i`* Re: 100 Random Single Variable Linear Equations2Mike Sanders
7 Dec 24 i  i `- Re: 100 Random Single Variable Linear Equations1Mike Sanders
7 Dec 24 i  `* Re: 100 Random Single Variable Linear Equations7Mike Sanders
7 Dec 24 i   `* Re: 100 Random Single Variable Linear Equations6Mike Sanders
7 Dec 24 i    `* Re: 100 Random Single Variable Linear Equations5Janis Papanagnou
8 Dec 24 i     `* Re: 100 Random Single Variable Linear Equations4Mike Sanders
8 Dec 24 i      `* Re: 100 Random Single Variable Linear Equations3Janis Papanagnou
9 Dec 24 i       `* Re: 100 Random Single Variable Linear Equations2Mike Sanders
9 Dec 24 i        `- Re: 100 Random Single Variable Linear Equations1Janis Papanagnou
8 Dec 24 +* Re: 100 Random Single Variable Linear Equations2Mike Sanders
8 Dec 24 i`- Re: 100 Random Single Variable Linear Equations1Mike Sanders
8 Dec 24 `- Re: 100 Random Single Variable Linear Equations1Mike Sanders

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal