4

I'm using the lgc package to generate random numbers.

\usepackage[first=-100, last=100]{lcg}

Assume I want to write the following equation

$$ a x^2 + b =0$$

where

$a,b$ % are random integers ranged in $[-100,100]$

the "+" sign should be "+" if b>0 (set just as an example here) and "-" if not. I put the following:

\def\a{\nbal} $$\a \qquad \a$$
$\nbal x ( \a ) \ifnum\value{\a} <0 {\ } \else {+}\fi \a $

The second line being my control. One can see that the "\def" acts as a macro. The question is "how to assign to a variable a value that can be static? I'm stuck. Any idea? Thx in advance. M Since I didn't manage to answer the request for a working example I'm editing:


\documentclass{article}

\usepackage[utf8]{inputenc} \usepackage[gen]{eurosym} \usepackage[x11names]{xcolor,colortbl} \usepackage[light, oldstyle, largesmallcaps]{kpfonts} \usepackage{ifthen} \usepackage[first=-100, last=100]{lcg} % \def\nbal{\rand\arabic{rand}} % \begin{document} % \def\a{\nbal} \def\b{\nbal} \def\c{\nbal} \def\d{\nbal} %write down what you have $$\a \qquad \a$$ $\nbal x \ifnum \value{\a} <0 {\ } \else {+}\fi \a $ % \end{document}

moon
  • 41
  • 2

2 Answers2

4

Although various packages, such as lgc, can be useful, they are usually limited in scope and you can be left wasting time trying to figure out other details (in your case how to avoid printing a +- if b is negative). An easy way around this is to find a better tool: the sagetex package lets you farm out mathematical content to a computer algebra system, Sage which just so happens to let you use Python (an easy to understand programming language which is great for working with strings). The power of LaTeX, Python, and Sage will be able to help you handle almost any mathematical issue easily. For your problem we can try the code below:

\documentclass{article}
\usepackage{sagetex}
\begin{document}
Suppose I have the equation \[ax^2+b=0\] where $a$ and $b$ are random integers 
between $-100$ and $100$, inclusive.
\begin{sagesilent}
a = randint(-100,100)
b = randint(-100,100)
if b<0:
    output = r"\[ %d x^2 - %d = 0\]"%(a,-b)
else:
    output = r"\[ %d x^2 + %d = 0\]"%(a,b)
\end{sagesilent}
For example, if $a = \sage{a}$ and $b = \sage{b}$ then the equation is: 
\sagestr{output}
\end{document}

The output running in Cocalc gives this random quadratic: enter image description here

The code isn't too difficult to read, even if you don't know Python. Picking up basic Python commands isn't difficult either. You can almost certainly find the line where a and b are chosen to be random integers between -100 and 100. As you observed in your question, if b is negative then the sign needs to change. That's done with and if-then-else statement. The less easily understood part is the line output = r"\[ %d x^2 - %d = 0\]"%(a,-b). The %d indicates an integer is to be put in that spot (%f for float and %s for string). There are 2 places that need an integer the %(a,-b) indicates that a will be in the first %d and -b in the second %d. This line is executed when b is negative typesetting a negative sign followed by -b, which is positive. The else part executes when b is positive, so + is typeset and b, which is positive, is inserted.

Sage is not part of LaTeX so either 1. download to your computer and get it to work with LaTeX (sometimes problematic) or 2. open up a free Cocalc account. Copy/paste and you're on your way in 5 minutes. EDIT: \sage{} is used to get numerical data, \sagestr{} to get string data, \sageplot{} for plots. Search this site for some of the many applications of the sagetex package.

DJP
  • 12,451
2

You're calling a new random integer at each call of \a.

Here's a different approach using expl3. Instead of using short commands for variables, which could override important commands (never do \def\c{...}, for instance), I prefer to use \rv{<name>}.

Values -1, 0 and 1 are discarded as they're would be awkward for the intended application.

\documentclass{article}

\ExplSyntaxOn \NewDocumentCommand{\definerandomvariable}{O{100}O{-100}m} { % allocate the variable or clear it \tl_clear_new:c { l_moon_rv_#3_tl } % set the value \moon_rv_define:nnn { #2 } { #1 } { #3 } } \NewExpandableDocumentCommand{\rv}{m} { \tl_use:c { l_moon_rv_#1_tl } } \NewExpandableDocumentCommand{\signedrv}{m} { \int_compare:nT { \tl_use:c { l_moon_rv_#1_tl } > 0 } { + } \tl_use:c { l_moon_rv_#1_tl } }

\cs_new_protected:Nn \moon_rv_define:nnn { \int_set:Nn \l_tmpa_int { \int_rand:nn { #1 } { #2 } } \int_compare:nTF { -1 <= \l_tmpa_int <= 1 } {% discard values -1, 0 and 1 \moon_rv_define:nnn { #1 } { #2 } { #3 } } {% set the value when not -1, 0 or 1 \tl_set:cx { l_moon_rv_#3_tl } { \int_eval:n { \l_tmpa_int } } } } \ExplSyntaxOff

\begin{document}

\definerandomvariable{a} \definerandomvariable[20][-10]{b} \definerandomvariable[-10][-100]{c}

$\rv{a} \qquad \rv{a}$

$\rv{b} x \signedrv{a}$

$\rv{b} x \signedrv{c}$

\end{document}

The command \definerandomvariable has two optional arguments, for the upper and lower bound respectively. The default values are 100 and -100 respectively.

I also provide \signedrv to print the variable with a + sign if positive.

enter image description here

In the example, c is negative just to test the code.

egreg
  • 1,121,712