Groups | Search | Server Info | Login | Register


Groups > comp.lang.awk > #9894

Re: 100 Random Single Variable Linear Equations

From porkchop@invalid.foo (Mike Sanders)
Newsgroups comp.lang.awk
Subject Re: 100 Random Single Variable Linear Equations
Date 2024-12-08 07:16 +0000
Organization A noiseless patient Spider
Message-ID <vj3h3m$3ldqc$1@dont-email.me> (permalink)
References <vits2o$240vr$1@dont-email.me> <vj3d23$3kkn4$1@dont-email.me>

Show all headers | View raw


Mike Sanders <porkchop@invalid.foo> wrote:

> and unless there's something out of whack, i'm using this version...

>         } 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)
>         }...

in form 2 (f==2) e is incorrect (op2...), but fixed now:

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 b2x = c
            b2 = rnd(1, 20) # new/different coefficient for 2nd x
            e  = sprintf("%s%dx %s %dx = %d", n, a, op1, b2, 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

Back to comp.lang.awk | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

100 Random Single Variable Linear Equations porkchop@invalid.foo (Mike Sanders) - 2024-12-06 03:46 +0000
  Re: 100 Random Single Variable Linear Equations porkchop@invalid.foo (Mike Sanders) - 2024-12-06 04:51 +0000
    Re: 100 Random Single Variable Linear Equations Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-12-06 13:43 +0100
      Re: 100 Random Single Variable Linear Equations Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-12-06 13:59 +0100
        Re: 100 Random Single Variable Linear Equations porkchop@invalid.foo (Mike Sanders) - 2024-12-07 02:39 +0000
        Re: 100 Random Single Variable Linear Equations porkchop@invalid.foo (Mike Sanders) - 2024-12-07 20:42 +0000
      Re: 100 Random Single Variable Linear Equations porkchop@invalid.foo (Mike Sanders) - 2024-12-06 13:38 +0000
        Re: 100 Random Single Variable Linear Equations Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-12-06 21:01 +0100
          Re: 100 Random Single Variable Linear Equations Christian Weisgerber <naddy@mips.inka.de> - 2024-12-06 23:05 +0000
            Re: 100 Random Single Variable Linear Equations porkchop@invalid.foo (Mike Sanders) - 2024-12-07 01:36 +0000
        Re: 100 Random Single Variable Linear Equations Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-12-06 12:53 -0800
          Re: 100 Random Single Variable Linear Equations porkchop@invalid.foo (Mike Sanders) - 2024-12-06 22:05 +0000
            Re: 100 Random Single Variable Linear Equations porkchop@invalid.foo (Mike Sanders) - 2024-12-07 02:06 +0000
        Re: 100 Random Single Variable Linear Equations porkchop@invalid.foo (Mike Sanders) - 2024-12-06 22:04 +0000
          Re: 100 Random Single Variable Linear Equations porkchop@invalid.foo (Mike Sanders) - 2024-12-07 01:49 +0000
            Re: 100 Random Single Variable Linear Equations Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-12-07 03:21 +0100
              Re: 100 Random Single Variable Linear Equations porkchop@invalid.foo (Mike Sanders) - 2024-12-08 06:40 +0000
                Re: 100 Random Single Variable Linear Equations Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-12-08 15:15 +0100
                Re: 100 Random Single Variable Linear Equations porkchop@invalid.foo (Mike Sanders) - 2024-12-09 16:43 +0000
                Re: 100 Random Single Variable Linear Equations Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-12-09 21:10 +0100
  Re: 100 Random Single Variable Linear Equations porkchop@invalid.foo (Mike Sanders) - 2024-12-08 06:06 +0000
    Re: 100 Random Single Variable Linear Equations porkchop@invalid.foo (Mike Sanders) - 2024-12-08 07:16 +0000
  Re: 100 Random Single Variable Linear Equations porkchop@invalid.foo (Mike Sanders) - 2024-12-08 11:02 +0000

csiph-web