Sujet : Re: 100 Random Single Variable Linear Equations
De : porkchop (at) *nospam* invalid.foo (Mike Sanders)
Groupes : comp.lang.awkDate : 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
-- :wqMike Sanders