Deutsch   English   Français   Italiano  
<vj3d23$3kkn4$1@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!weretis.net!feeder9.news.weretis.net!news.quux.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: porkchop@invalid.foo (Mike Sanders)
Newsgroups: comp.lang.awk
Subject: Re: 100 Random Single Variable Linear Equations
Date: Sun, 8 Dec 2024 06:06:59 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 65
Sender: Mike Sanders <busybox@sdf.org>
Message-ID: <vj3d23$3kkn4$1@dont-email.me>
References: <vits2o$240vr$1@dont-email.me>
Injection-Date: Sun, 08 Dec 2024 07:07:02 +0100 (CET)
Injection-Info: dont-email.me; posting-host="0513f042c00441571518d761624f4bea";
	logging-data="3822308"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19IcAdyt1VJIFMrSa8i9pNJ"
User-Agent: tin/2.6.2-20221225 ("Pittyvaich") (NetBSD/9.3 (amd64))
Cancel-Lock: sha1:BUzyM8PMrjCb7SK3dEit5HMZjzc=
Bytes: 3114

Mike Sanders <porkchop@invalid.foo> wrote:

> # outputs 100 random single variable linear equations in the form: ax+b=c

now randomly creates up to 2 instances of 'x' per equation & 3 random forms...

ax op1 b = c
ax op1 bx = c
ax op1 b op2 x = c

(really more than 3 if there's a leading negtive)

and unless there's something out of whack, i'm using this version, because
algebra can melt my brain as it increases in complexity...

BEGIN {
    # seed random number generator
    if (SEED+0 != SEED) SEED = 1; srand(SEED)

    # keep generating until we have exactly 100 unique equations
    do {
        a = rnd(1, 20)                  # random value for coefficient x
        b = rnd(1, 99)                  # random value for b constant
        c = rnd(1, 99)                  # random value for c constant
        n = (rnd(1, 2) == 1) ? "-" : "" # random negative for coefficient x
        f = rnd(1, 3)                   # random equation form
        op1 = rop()                     # random operator

        if (f == 1) {
            # simple equation: ax op1 b = c
            e = sprintf("%s%dx %s %d = %d", n, a, op1, b, c)
        } else if (f == 2) {
            # medium complexity: ax op1 bx = c
            b2  = rnd(1, 20) # new/different coefficient for 2nd x
            op2 = rop()      # 2nd random operator
            e   = sprintf("%s%dx %s %dx = %d", n, a, op1, b2, op2, c)
        } else if (f == 3) {
            # more complex: ax op1 b op2 x = c
            op2 = rop() # 2nd random operator
            e   = sprintf("%s%dx %s %d %s x = %d", n, a, op1, b, op2, c)
        }

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

    } while (u < 100)

    # print seed & equations
    printf("SEED: %d\n\n", SEED)
    for (j in equ) printf("%03d. %s\n", ++i, j)
}

function rop() { return substr("+-*/", rnd(1, 4), 1) }

function rnd(min, max) { return int(rand() * (max - min + 1)) + min }

# eof

-- 
:wq
Mike Sanders