LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-14-2017, 09:04 AM   #1
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
random BASH script algorithms


I understand why RANDOM is NOT random due its math behind it.

I have a BASH Script I have written long ago. I use it to work - or - manipulate files within as many different directories I want. Within all of the directories are image files.

the image files are used to display on the desktop screen background.

the script "randomizes" the files so that a different one is picked each time then displayed onto the desktop.

I use one IF statement to cycle through a case statement, then another case statement that uses the line of commands needed to display the image and within that line of commands I use shuf to select the image within whatever directory that is picked to use.

As anyone that has worked with anything that preforms a random "number" and lets it run - repeat the process again and again will notice that it is no longer being random. Rather the same numbers are being generated over and over again.

It has "metamorphose" into a pattern of seemingly random numbers.


as my script is really long because I use RANDOM itself in functions to change some of the arguments in the line of commands. If I post my BASH Script in its entirety it may easily confuse most people.

So I will only post the if and two case statements, if one wants to see more then just submit the proper paper work and I will post it in here.


The question is. does anyone know of a different or better means of using any type of means that uses random or an algorithm that will work in bash script and I can use to produce the same results of an absolute path and file name.jpg (png) to be returned for use?

This is a shortened version so you the reader will hopefully get the idea of how this is being done.

Code:
 while true; do   
		
		if [[ "$wpaper" -eq 4 ]] ; then
		  {
		    wpaper=1
		   }
		   else
		   {
		      ((++wpaper))
		  }
		 fi
		 
		 case $wpaper in
		 1)
		 {
			 WALLPAPERS=/media/data/Naked-Fish
			 setpaper=1
		 }
		 ;;
		 2)
		 {
			 WALLPAPERS=/usr/share/wallpapers/
			 setpapers=2
		 }
		 ;;
		 3)
		 {
			WALLPAPERS=/media/data/Dead-Flowers
			setpaper=1
		 }
		 ;;
		 4)
		 {
		   #just colors
		    setpaper=3
		 }
		 ;;
		esac
		 
		 case $setpaper in
		 1)
		 {
		    find "$WALLPAPERS" -type f \( -name '*.jpg' -o -name '*.png' \) -print0 | shuf -n 15 -z | shuf -n 1 -z | xargs -0 \
		    mhsetroot -addd $(color) 3 -addd $(color2) 5 -addd $(color) 3 -addd $(tint) 8 -gradient "$(setGradient)" -fill 
		 }
		 ;;
		 2)
		 {
		     find "$WALLPAPERS" -type f \( -name '*.jpg' -o -name '*.png' \) -print0 | shuf -n 1 -z | xargs -0 mhsetroot -fs 
		 }
		 ;;
		 3)
		 {	
		   mhsetroot -addd $(color) 3 -addd $(color2) 5 -addd $(color) 3 -addd $(tint) 8 -gradient "$(setGradient)"
		 }
		 ;;
		 esac
		 
		#notes:
		#find "$WALLPAPERS" -type f \( -name '*.jpg' -o -name '*.png' \) -print0 | shuf -n 3 -z | xargs -0 \
		#mhsetroot -addd $(color) 3 -addd $(color2) 5 -addd $(color) 3 -addd $(tint) 8 -gradient "$(setGradient)" -fill 
		
		
		
		#mhsetroot -addd $(color) "$(addSetNum1)" -addd $(color2) "$(addSetNum2)" -gradient "$(setGradient)" \
               
                # to set image to user specified size 
		#-dcenter 300x300
		# -tint $(tint) -brightness "$(setContrast)" -dcenter 700x450
		
		# ${setS} "${setI}" 450x500
		#changes options ie -tile -fill -dia -center -flipimg then add the prams needed after
			
		echo "sleep time changed to $(($nw/60)) minutes"
			
		sleep "$nw"
		
	
	done &

Last edited by BW-userx; 04-14-2017 at 09:24 AM.
 
Old 04-14-2017, 10:36 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
1. do not need to use { and } within if/then/else and case/esac.
2. assuming the files will not be changed you can execute find only one time, store the images in an array and you only will need to generate one random number to select an image.
 
Old 04-14-2017, 12:03 PM   #3
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
recently i found this:
Code:
openssl rand
it has various options.
2 examples:
Code:
openssl rand -base64 128
openssl rand -hex 3
not sure how truly random it is.
read the docs.
 
1 members found this post helpful.
Old 06-16-2017, 02:07 AM   #4
sweepnine
LQ Newbie
 
Registered: Jun 2017
Posts: 16

Rep: Reputation: Disabled
You may use /dev/urandom instead of the special parameter RANDOM. With od you can turn /dev/randoms into a number.

Code:
head -c 2 /dev/urandom | od -A n-d
The output starts with a space that you may need to get rid off.

Last edited by sweepnine; 06-25-2017 at 05:36 PM.
 
Old 06-16-2017, 07:43 AM   #5
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by sweepnine View Post
You may use /dev/urandom instead of the special parameter RANDOM. With od you can turn /dev/randoms into a number.

Code:
head -c 2 /dev/urandom | od -A n-d
The output comes starts with a space that you may need to get rid off.
them sum big arse numbers
Code:
userx%slackwhere ⚡ ~ ⚡> randy=$(head -c 2 /dev/urandom | od -A n-d)
userx%slackwhere ⚡ ~ ⚡> echo $randy
110605
userx%slackwhere ⚡ ~ ⚡> randy=$(head -c 2 /dev/urandom | od -A n-d)
userx%slackwhere ⚡ ~ ⚡> echo $randy
157374
userx%slackwhere ⚡ ~ ⚡> randy=$(head -c 2 /dev/urandom | od -A n-d)
userx%slackwhere ⚡ ~ ⚡> echo $randy
154652
userx%slackwhere ⚡ ~ ⚡>
 
Old 06-16-2017, 02:19 PM   #6
Myk267
Member
 
Registered: Apr 2012
Location: California
Posts: 422
Blog Entries: 16

Rep: Reputation: Disabled
How about using Perl to make the random part a lot easier?
Code:
#!/usr/bin/env bash
#set -xe
declare -a myDirs=(
$HOME/Downloads 
$HOME/Documents 
$HOME/Pictures
)
Wallpapers=${myDirs[ $(perl -E 'say int(rand($ARGV[0]))' ${#myDirs[@]}  ) ]}
find $Wallpapers -type f -name *.png -o -name *.jpg | shuf -n 1
 
Old 06-25-2017, 05:35 PM   #7
sweepnine
LQ Newbie
 
Registered: Jun 2017
Posts: 16

Rep: Reputation: Disabled
Sorry I made a mistake. od outputs octal numbers of course. Fix this with bc:

Code:
head -c 2 /dev/urandom | echo "ibase=8; $(od -A n-d)" | bc
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script? how to rename files in random way? lefty.crupps Linux - Software 16 11-03-2015 02:07 PM
[SOLVED] bash: create random script with select case thecazz Programming 8 08-04-2013 12:29 PM
BASH Script and $RANDOM lupusarcanus Programming 2 03-15-2011 02:24 AM
killing child processes of a bash script results in strange random kills omnio Programming 6 03-12-2007 07:35 AM
BASH script showing random behavior chatterbug89 Programming 12 07-19-2005 10:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration