1

I'm trying to generate random datetime with a code I found on the net. This code made use of $RANDOM. The strange thing is, I could keep executing the script but the datetime result would never change. But when I add "echo $Random", the datetime result changes. Is it something to do with seeding of random variable and executing "echo" somehow reset it?

#echo $RANDOM // This causes desire output
x=$(date -d "$((RANDOM%1+2010))-$((RANDOM%12+1))-$((RANDOM%28+1)) $((RANDOM%23+1)):$((RANDOM%59+1)):$((RANDOM%59+1))" '+%d-%m-%Y %H:%M:%S')
echo $x

OUTPUT

INT,1,100128,2018/05/24 08:00:00,1,0,Test
INT,1,100128,2018/05/24 08:00:00,1,0,Test
INT,1,100128,2018/05/24 08:00:00,1,0,Test
INT,1,100128,2018/05/24 08:00:00,1,0,Test
KMC
  • 1,677
  • 3
  • 26
  • 55
  • Show output of `echo $BASH_VERSION`. – Cyrus Oct 03 '18 at 14:34
  • I'm using Babun (Windows Shell) – KMC Oct 03 '18 at 14:37
  • I attached screencap of my result – KMC Oct 03 '18 at 14:53
  • @AmitBhardwajIN I deleted my comment because I couldn't test this with the user's environment. I don't know anything about Babun, which could be to blame somehow. – chepner Oct 03 '18 at 15:01
  • 1
    I would be surprised if this works (but then, I'm surprised it doesn't already work), but what happens if you use `$RANDOM` instead of `RANDOM` inside the arithmetic expressions? – chepner Oct 03 '18 at 15:02
  • @chepner It doesn't change the behavior at all. – KMC Oct 03 '18 at 15:13
  • OK, nothing specific to *`bash`* can explain what's going on; it's got to be some implementation detail of Babun. Just to confirm, if you uncomment `echo $RANDOM`, do you get different random values each time you run the script, or do you get the same fixed *stream* of random values in each run? – chepner Oct 03 '18 at 15:31
  • Each section (delimited by a blue line) represents one random generation and one time execution of the script. I'm dumping my output to a text file. Above screencap shows 3 times execution of the script and 3 random generation. Sorry for confusion. – KMC Oct 03 '18 at 15:35
  • the code works for me. Every invocation produces a different date like `28-03-2010 15:33:28` in year 2010 – matzeri Oct 03 '18 at 17:07
  • 1
    The year will always be 2010 because `RANDOM%1` is always zero. – pjh Oct 03 '18 at 18:25
  • 1
    See the ['bash' tag info page](https://stackoverflow.com/tags/bash/info), particularly the part about providing a "small, self-contained example". To maximize your chances of getting a useful answer you need to provide working code and the output that it produces. The code that generated the output in the screen capture has not been provided. If the code is too complex (or sensitive) to post here, you should cut it down to the minimum needed to demonstrate the problem. Doing that might reveal the cause of the problem (it often does). – pjh Oct 03 '18 at 19:21
  • It would be more useful to provide the output as text. See [Discourage screenshots of code and/or errors](https://meta.stackoverflow.com/q/303812/4154375). – pjh Oct 03 '18 at 19:27
  • the code I posted has only one difference from actual code - the echo statement produce some hardcoded values and randomly generated datetime instead of echoing just randomly generated datetime. – KMC Oct 05 '18 at 14:30

1 Answers1

1

For an answer to the question about seeding and the effect of echo, see $RANDOM: generate random integer in the Advanced Bash-Scripting Guide. In short, the $RANDOM value is (supposed to be) automatically seeded to a random value on each run of Bash, but it can be reseeded by setting it to a constant value if you need to have it produce the same sequence of numbers multiple times. Running echo $RANDOM won't reseed $RANDOM, but it will cause it to move to the next "random" value if it has been seeded already.

As far as I can tell, Babun is just Cygwin with some added tools and configurations. I have not seen anything like the problem that the OP is reporting with Cygwin Bash. Assuming that the OP is running Bash (and not Zsh, which Babun also seems to provide), the most likely cause of the problem is that something is reseeding $RANDOM to a constant value. I suggest double-checking the code to ensure that there are no assignments to RANDOM. Once that has been ruled out, the next obvious suspect is one of the Babun startup files for Bash, because it claims to do shell customizations.

If whatever is reseeding RANDOM cannot be identified or fixed, the only option that comes to mind is to reseed it expicitly in the code to something reasonably random. The article referenced above suggests

RANDOM=$$

That is very weak, but it might be good enough in this case. If it isn't, there are other suggestions in the article referenced above (e.g. involving /dev/urandom). Also see Why is Bash’s $RANDOM not seeded (?) on some machines?, Unix $RANDOM function not random in Bash terminal, and $RANDOM in linux bash will alway be the same upon bootup.

pjh
  • 6,388
  • 2
  • 16
  • 17
  • Thanks for your thorough response. To answer couple of your questions, yes I'm using Bash not ZSh. And the code I posted has only one difference from actual code - the echo statement produce some hardcoded values and randomly generated datetime instead of echoing just randomly generated datetime. I haven't had time to look into all of your suggestions..but when I run the same script on Ubuntu server and git shell, it works just fine. But I will look into your suggestions for future reference. Thanks for all your help. – KMC Oct 05 '18 at 14:29