Paul Sutton

random

Bash scripting 14 – Generating data files

Issue 249, May 2025 of Linux Magazine, p52-55, had an article on using awk, and gnu plot to manipulate data. In order to try and make use of this, and learn more, I am trying to figure out how to write a csv file.

Write 10 lines to the first column should be in sequence e.g. 1-10 and the 2nd column should be random numbers. This should be written to a file.

So after some digging I managed to come up with this, by modifying some existing solutions

write_csv(){
    echo \"$1\",\"$2\" > log.csv # write to file, use >> to append
}

#sources of help
echo "number" "data" # for reference, not written to file 
for ((i = 0 ; i < 10 ; i++)); do
    echo $i, $RANDOM # echo to screen (stdout)
	echo $i, $RANDOM >> log.csv # write to file
    #write_csv $(($i, $RANDOM % 10))
done

The screen output for me was

number data
0, 17109
1, 2872
2, 22572
3, 7228
4, 27923
5, 6335
6, 20004
7, 32343
8, 10005
9, 21905

While the file output (log.csv) was similar but without the headings, which I just put in for my own reference, the file does work as a csv as I can open in LibreOffice Calc.

0, 17109
1, 2872
2, 22572
3, 7228
4, 27923
5, 6335
6, 20004
7, 32343
8, 10005
9, 21905

As the numbers are quite big, a closer examination, promoted me to make the following modification

echo $i, $((RANDOM % 20)) # echo to screen (stdout)
echo $i, $((RANDOM % 20)) >> log.csv # write to file

This fixes the 2nd column to produce a number between 1 and 20.

number data
0, 17
1, 12
2, 12
3, 16
4, 2
5, 13
6, 10
7, 6
8, 16
9, 16
cat log.csv
0, 3
1, 17
2, 8
3, 16
4, 10
5, 18
6, 5
7, 14
8, 11
9, 11

The script was called gencsv1.sh, which as expected needed execute permissions

chmod +x gencsv.sh

References

In order to help me I used the following, so giving credit to these sources as would be expected.

Tags

#Bash,#Bashscripting,#Files,#Random,#Data,#CSV,


Mastodon ShellLabs Join Mastodon
AI statement : Consent is NOT granted to use the content of this blog for the purposes of AI training or similar activity. Consent CANNOT be assumed, it has to be granted.

Donate using Liberapay

Rocks and Diamonds – Level Creation 3

Placing the same element, multiple times, in the playing area, in random positions.

As mentioned my previous post I am going to briefly discuss random placement of elements on the playing area.

It is important this point to stress that the game editor will randomly place the selected element multiple times, and NOT just place random elements on the playing area.

On the game editor find the following block.

random placement 1

Within this there is a random placement button, depicted by a dice. Select this:

random placement 2

If you now click on one of the game elements for example a green emerald, and press the dice the editor will place several on the playing area.

random placement 3

You don't need to keep everything where the editor puts it, feel free to delete and add others manually elsewhere, but where you need lots of one thing it may help.

This is one of the features you end up wishing you had known about, especially when you have spent weeks, creating levels by manually placing the items multiple times.

Don't for get to add your exit block manually.

Placing lines of blocks.

The third button in, is a line drawing tool. As this sort of relates to the above, I will cover this too. Pressing the line button and then selecting, for example a wall element, you can draw straight lines. This makes it quicker and easier to draw longer walls or even mazes for example.

#games, #levels, #rocksndiamonds, #level,#creation,#codeclub, #random


cc-by logo

Licenced under Attribution 4.0 International (CC BY 4.0)


Mastodon ShellLabs Join Mastodon
AI statement : Consent is NOT granted to use the content of this blog for the purposes of AI training or similar activity. Consent CANNOT be assumed, it has to be granted.

Donate using Liberapay