Julia cannot reproduce the rand - random

I am just reading and practicing the "3.5.2.1 rand" section of https://juliadatascience.io/standardlibrary and found the code below cannot reproduce same random numbers:
$ julia
_
_ _ _(_)_ | Documentation: https://docs.julialang.org
(_) | (_) (_) |
_ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
| | | | | | |/ _` | |
| | |_| | | | (_| | | Version 1.7.0 (2021-11-30)
_/ |\__'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |
julia> using Random: rand, randn, seed!
julia> my_seed = seed!(123)
Random.TaskLocalRNG()
julia> rand(my_seed, 3)
3-element Vector{Float64}:
0.521213795535383
0.5868067574533484
0.8908786980927811
julia> rand(my_seed, 3)
3-element Vector{Float64}:
0.19090669902576285
0.5256623915420473
0.3905882754313441
The snapshot of the book:

Well spotted. Running rand(my_seed, 3) twice shouldn't give the same output. This problem was caused by a bug in Books.jl. The Books package stores outputs in files as a sort of caching mechanism. Unfortunately, two identical blocks of code will be written to the same file path, so the output for the last block overrides the output for the first block. I still haven't found a nice solution to that problem and normally it isn't a problem because the same code will usually give the same output, but not for calling rand.
This problem will be fixed in the HTML and PDF version on the website about 30 minutes after https://github.com/JuliaDataScience/JuliaDataScience/pull/235 is merged.

You're missing the second call to seed! that's in the book.

Related

Generate English words using special characters in the comments

I want to generate a English word with pure characters like |, /, \, (, ) etc. in my code comments.
For example,
_
(_)
__ __ __ _ ___ ___ _
\ \ / / / _` | / __| / __| | |
\ V / | (_| | | (__ | (__ | |
\_/ \__,_| \___| \___| |_|
I wonder if there is a tool that automatically does the job? I don't know how to properly google it.
This tool might be helpful: https://patorjk.com/software/taag/#p=display&h=1&v=1&f=Big&t=vaccine
You can choose your favorite font, character width/height, and copy the wonderful ASCII art to your clipboard.

write to a file with a specific font size

I want to write to a file in bash but I wan to use a specific font size. For example, I want to write to the file hello but with a font size of 30.
echo "Hello "
Also, is there another way to indent in bash when writing to a file besides using spaces like below?
echo " Hello"
As #thatotherguy commented, details like fonts are determined by the program reading the file, so it depends on what sort of file you're creating, and you'll typically use different tools to create different types of files. echo and other shell commands just work with text; the literal characters h, e, l, l, and o are sent to the terminal, no size or font data goes along with it.
If you're trying to simply make big font in your terminal there are tricks, like ASCII art text using figlet:
$ figlet "Hello"
_ _ _ _
| | | | ___| | | ___
| |_| |/ _ \ | |/ _ \
| _ | __/ | | (_) |
|_| |_|\___|_|_|\___/
And since you asked about indentation, notice that figlet supports centered text and other sorts of formatting:
figlet -c "Hello"
_ _ _ _
| | | | ___| | | ___
| |_| |/ _ \ | |/ _ \
| _ | __/ | | (_) |
|_| |_|\___|_|_|\___/
It is possible to make some changes to the text that gets displayed in your terminal (you've probably seen colored text before from some commands), but not fonts. You can use tput to modify the text you output, e.g. with colors or bold, but not size or font (which are configured by your terminal itself). Some examples of that in this question, but tput is easier than figuring out all the \e... escape sequences they're talking about.
# it looks plain here, but if you run this in your shell it ought to be underlined
$ echo "$(tput smul)hello$(tput rmul)"
hello
If ASCII art or the color/font features most terminals support isn't what you're looking for, you'll need to share more details about what you're trying to do.

How to print ASCII picture/logo in console using bash

I have a logo which I want to print/echo using bash in console, through scripts, but I can't even paste here properly(looks good inside .txt file):
___ _____ _ _ _____ _ _ ___
| _| / ___| | | || ___| \ | | |_ |
| | \ `--.| |_| || |__ | \| | | |
| | `--. \ _ || __|| . | | |
| | /\__/ / | | || |___| |\ | | |
| |_ \____/\_| |_/\____/\_| \_/ _| |
|___| |___|
How I can do that properly and make it colour without 3rd party extensions?
Also, is it possible to print it from the file?
You may use tput:
tput setaf 2; cat logo.txt
2 is the color code here (red). These are the available codes:
Value Color
0 Black
1 Red
2 Green
3 Yellow
4 Blue
5 Magenta
6 Cyan
7 White
8 Not used
9 Reset to default color

how to exit Gallium debugger?

what's the command to exit the Gallium debugger for Julia? I tried all things like q, Q, exit(), exit, Ctrl-c.
_ _ _(_)_ | A fresh approach to technical computing
(_) | (_) (_) | Documentation: http://docs.julialang.org
_ _ _| |_ __ _ | Type "?help" for help.
| | | | | | |/ _` | |
| | |_| | | | (_| | | Version 0.5.0 (2016-09-19 18:14 UTC)
_/ |\__'_|_|_|\__'_| | Official http://julialang.org/ release
|__/ | x86_64-apple-darwin13.4.0
julia> using Gallium
julia> include(Pkg.dir("Gallium/examples/testprograms/misc.jl"))
optional (generic function with 2 methods)
julia> Gallium.breakpoint(sinthesin,Tuple{Int64})
Locations (+: active, -: inactive, *: source):
+ sinthesin(x::Int64) at /Users/florian.oswald/.julia/v0.5/Gallium/examples/testprograms/misc.jl:2
julia> inaloop(2)
In /Users/florian.oswald/.julia/v0.5/Gallium/examples/testprograms/misc.jl:2
1 #noinline function sinthesin(x)
2 sin(sin(x))
3 end
4
About to run: Main.sin
1|debug > q
In /Users/florian.oswald/.julia/v0.5/Gallium/examples/testprograms/misc.jl:2
1 #noinline function sinthesin(x)
2 sin(sin(x))
3 end
4
About to run: Main.sin
1|debug >
UPDATE
there is a related issue on github. The main takeaway from this is to use #enter func(x,y) and step into a function directly. i find this works pretty well.
When working with REPL, you can use finish to exit the current function. To exit debugger, you may need to execute this command several times.
In Atom, the corresponding command is the Debug: Finish Function button.
You can find more commands here.
UPDATE:
A breakpoint pauses the program whenever a certain point in the program is reached. In your example, inaloop(2) will call sinthesin twice, so a single q or Ctrl+d is not enough to exit the debugger. I think this is the expected behavior, take a look at the following example:
julia> bp = Gallium.breakpoint(sinthesin,Tuple{Int64})
Locations (+: active, -: inactive, *: source):
+ sinthesin(x::Int64) at /Users/gnimuc/.julia/v0.5/Gallium/examples/testprograms/misc.jl:2
julia> inaloop(2)
In /Users/gnimuc/.julia/v0.5/Gallium/examples/testprograms/misc.jl:2
1 #noinline function sinthesin(x)
2 sin(sin(x))
3 end
4
About to run: Main.sin
1|debug > q
In /Users/gnimuc/.julia/v0.5/Gallium/examples/testprograms/misc.jl:2
1 #noinline function sinthesin(x)
2 sin(sin(x))
3 end
4
About to run: Main.sin
1|debug > q
julia>
julia> Gallium.disable(bp)
true
julia> #enter inaloop(2)
In /Users/gnimuc/.julia/v0.5/Gallium/examples/testprograms/misc.jl:6
5 #noinline function inaloop(y)
6 for i = 1:y
7 sinthesin(i)
8 end
About to run: (colon)(1,2)
1|debug > q
Adding to Gnimuc's answer you should also be able to exit the debugger with the q command.
From here:
const all_commands = ("q", "s", "si", "finish", "bt", "loc", "ind", "shadow",
"up", "down", "ns", "nc", "n", "se")

pass a variable into a ascii art

In bash/ Ubuntu,
If there is a ASCII art file: "ascii-art" in the following
|__ __| ____|/ ____|__ __|
| | | |__ | (___ | |
| | | __| \___ \ | |
| | | |____ ____) | | |
|_| |______|_____/ |_| Client ${CLIENT_ID}
Is there anyway to pass the variable "${CLIENT_ID}" into the ascii-art every time we call it?
The way we call it at the moment:
cat ascii-art
The following ways don't work
1. cat ascii-art | CLIENT_ID="1"
or
Add one more line in the first line of the file "ascii-art"
. command_line_parse.sh -c ${CLIENT_ID}
|__ __| ____|/ ____|__ __|
| | | |__ | (___ | |
| | | __| \___ \ | |
| | | |____ ____) | | |
|_| |______|_____/ |_| Client ${CLIENT_ID}
Then
cat ascii-art | CLIENT_ID="1"
Could any guru enlighten? Thanks.
The envsubst tool is written for exactly this purpose:
CLIENT_ID=foo envsubst ascii-art
This is a preferable tool to sed, which restricts the range of possible values (if you had a / in your id, or even worse a semicolon followed by another sed command, serious bugs could ensue).
See also TemplateFiles on the Wooledge wiki, which includes a native-bash implementation for systems without GNU gettext (which includes envsubst).
The following evaluates the echo command for the contents of the file:
CLIENT_ID=1000 eval "$(cat <<EOC
echo -e "$(<ascii-art)"
EOC
)"
CLIENT_ID=1000 assigns environment variable for the eval command
eval accepts a here document as its single argument
$(<ascii-art), in Bash, does the same as $(cat ascii-art)
EDIT regarding the scary eval
It's true that we should avoid using eval. But we should also understand the
purpose of the command and do use it when appropriate. We should understand
the security risks, and decide whether we should, or we shouldn't use eval
in certain situations.
The Bash Hackers Wiki gives a good description
of eval:
Perhaps the easiest way to think about eval is that it works in the same way
as running bash -c "bash codeā€¦" from a script, except in the case of
eval, the given code is executed in the current shell environment rather
than a child process.
So eval just executes the shell code we pass. How often we execute
external commands from Bash scripts? I guess, quite often. And the commands
are just trusted executables which may well be Bash scripts themselves.
Then why should one be scared of evaluating some echo "trusted content"?
It is up to the user(OP) to decide whether it is safe to use eval in certain
situation. However, this answer definitely gives him an option; it is an
alternate solution. So I don't understand the downvote on this answer.
First: look at the programs figlet, toilet or cowasy. These are probably finished implementation of what you want.
If you want to write it yourself:
sed 's/${CLIENT_ID}/42/g' ascii-art
Modified from RotatingPieces' answer above
First modify the "ascii-art" file, and change "${CLIENT_ID}" into "CLIENT_ID_ART"
|__ __| ____|/ ____|__ __|
| | | |__ | (___ | |
| | | __| \___ \ | |
| | | |____ ____) | | |
|_| |______|_____/ |_| Client CLIENT_ID_ART
Now the following approach might not be very elegant, but it works perfectly
CLIENT_ID="1" # The ${CLIENT_ID} will actually be from the command line parser
echo "sed 's/CLIENT_ID_ART/"${CLIENT_ID}"/g' ascii-art > call_art
chmod +x call_art
./call_art &
rm -f call_art
The above will print out the following in the output
|__ __| ____|/ ____|__ __|
| | | |__ | (___ | |
| | | __| \___ \ | |
| | | |____ ____) | | |
|_| |______|_____/ |_| Client 1

Resources