Sponsored Content
Operating Systems OS X (Apple) Generate a random number in a fully POSIX compliant shell, 'dash'... Post 303043132 by wisecracker on Sunday 19th of January 2020 04:34:56 PM
Old 01-19-2020
Well the C source does not compile on a current update, 19-01-2020, Linux Mint 19 using gcc 7.4.0 so here is a completely simplified and modified version for gcc 4.2.1 and 7.4.0...
The printf function will need to edited for it to work.
It is set up for Linux, (Mint 19), using gcc 7.4.0.
You can still leave to original as it is for OSX 10.14.6+ as it won't affect the performance at all, but you can edit this in instead and have both capable with the minor alteration shown...
Code:
/* 'epoch_microsecs.c' */
/* Thanks to Perderabo for this little snippet modified to suit my needs. */
/* It saved me the bother of working it out by myself. */
/* https://www.unix.com/programming/1991-time-microseconds-2.html */

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>

int main(void)
{
    struct timeval tv;
    gettimeofday(&tv, NULL);

    /* Line below for gcc version 4.2.1, OSX 10.14.6... */
    /* printf("%ld.%06d", tv.tv_sec, tv.tv_usec); */

    /* Line below for gcc version 7.4.0, Linux Mint 19... */
    printf("%ld.%06ld", tv.tv_sec, tv.tv_usec);

    exit(0);
}


Last edited by wisecracker; 01-19-2020 at 06:46 PM.. Reason: Copy and paste error...
 

10 More Discussions You Might Find Interesting

1. Programming

How to generate a random number?

How to generate a random integer with specific range(for example, from 1 to 1000)? Also, how to convert a floating point number into a integer? (2 Replies)
Discussion started by: MacMonster
2 Replies

2. Shell Programming and Scripting

generate random number in korn shell

I want to be able to generate a random number within a korn shell script.. Preferably i would like to be able to state how many digits should be in this random number... ie 4 digits or 5 digits etc Any ideas? (2 Replies)
Discussion started by: frustrated1
2 Replies

3. Shell Programming and Scripting

generate random number in perl

Could any one tell how can I generate random number from (0, 100..200) in perl? Thanks! (2 Replies)
Discussion started by: zx1106
2 Replies

4. Programming

Generate random number

I saw this formula to generate random number between two specified values in shell script.the following. $(((RANDOM%(max-min+divisibleBy))/divisibleBy*divisibleBy+min)) Give a example in book. Generate random number between 6 and 30.like this. $(((RANDOM%30/3+1)*3)) But I have a... (1 Reply)
Discussion started by: luoluo
1 Replies

5. Shell Programming and Scripting

Unix random number generate in given range

Hi All, I have extracted some report from database for few activities done. Now I have a requirement to add some random time(In range of 10-35) in front of each activity. Can be generated random numbers in any bash/sh shell within a given number range, let's say in between 10-30. ... (10 Replies)
Discussion started by: gr8_usk
10 Replies

6. Shell Programming and Scripting

Logical expression in POSIX compliant Korn Shell

Hi, i want to check if a variable var1 is not a or b or c pseudo code: If NOT (var1 = a or var1 = b or var1 = c) then ... fi I want to use POSIX complaint Korn shell, and for string comparison For the following code, logical.sh #!/usr/bin/ksh var="j" echo "Var : $var" if ! { || ||... (12 Replies)
Discussion started by: ysrini
12 Replies

7. UNIX for Dummies Questions & Answers

how to generate random number as as the first column of a txt file

Dear all, I have a question. I have a txt file say 4000 rows X 1800 Column. I 'd like to creat a new column as the first column which is a column of random numbers (n=4000) thanks a lot! Lin (2 Replies)
Discussion started by: forevertl
2 Replies

8. Shell Programming and Scripting

Help with generate a pair of random number

Hi, Is anybody experience generate a pair of random number by using awk command? I wanna to generate a pair of random number (range from 1 to 4124) and repeats it 416 times. Desired output 2 326 123 1256 341 14 3245 645 . . . I did write the below command: awk... (5 Replies)
Discussion started by: perl_beginner
5 Replies

9. Shell Programming and Scripting

Generating a POSIX random number?

Hi Guys and gals... As you know I am getting to grips with POSIX and hit this stumbling block. Generating two random numbers 0 to 255 POSIXly. Speed in not important hence the 'sleep 1' command. I have done a demo that works, but it sure is ugly! Is there a better way? #!/bin/sh # Random... (12 Replies)
Discussion started by: wisecracker
12 Replies

10. Shell Programming and Scripting

Q: Is SQRT(n) possible in a POSIX compliant shell? A: Yes within limits.

Hi all... This is just a fun project to see if it is possible to get a square root of a positive integer from 1 to 9200000 to 6 decimal places on a 64 bit architecture machine. It is coded around dash and the results show the values from 0 to 10000. Complex numbers can easily be catered for by... (3 Replies)
Discussion started by: wisecracker
3 Replies
GETTIMEOFDAY(2) 					     Linux Programmer's Manual						   GETTIMEOFDAY(2)

NAME
gettimeofday, settimeofday - get / set time SYNOPSIS
#include <sys/time.h> int gettimeofday(struct timeval *tv, struct timezone *tz); int settimeofday(const struct timeval *tv , const struct timezone *tz); DESCRIPTION
The functions gettimeofday and settimeofday can get and set the time as well as a timezone. The tv argument is a timeval struct, as speci- fied in /usr/include/sys/time.h: struct timeval { long tv_sec; /* seconds */ long tv_usec; /* microseconds */ }; and gives the number of seconds and microseconds since the Epoch (see time(2)). The tz argument is a timezone : struct timezone { int tz_minuteswest; /* minutes W of Greenwich */ int tz_dsttime; /* type of dst correction */ }; The use of the timezone struct is obsolete; the tz_dsttime field has never been used under Linux - it has not been and will not be sup- ported by libc or glibc. Each and every occurrence of this field in the kernel source (other than the declaration) is a bug. Thus, the following is purely of historic interest. The field tz_dsttime contains a symbolic constant (values are given below) that indicates in which part of the year Daylight Saving Time is in force. (Note: its value is constant throughout the year - it does not indicate that DST is in force, it just selects an algorithm.) The daylight saving time algorithms defined are as follows : DST_NONE /* not on dst */ DST_USA /* USA style dst */ DST_AUST /* Australian style dst */ DST_WET /* Western European dst */ DST_MET /* Middle European dst */ DST_EET /* Eastern European dst */ DST_CAN /* Canada */ DST_GB /* Great Britain and Eire */ DST_RUM /* Rumania */ DST_TUR /* Turkey */ DST_AUSTALT /* Australian style with shift in 1986 */ Of course it turned out that the period in which Daylight Saving Time is in force cannot be given by a simple algorithm, one per country; indeed, this period is determined by unpredictable political decisions. So this method of representing time zones has been abandoned. Under Linux, in a call to settimeofday the tz_dsttime field should be zero. Under Linux there is some peculiar `warp clock' semantics associated to the settimeofday system call if on the very first call (after boot- ing) that has a non-NULL tz argument, the tv argument is NULL and the tz_minuteswest field is nonzero. In such a case it is assumed that the CMOS clock is on local time, and that it has to be incremented by this amount to get UTC system time. No doubt it is a bad idea to use this feature. The following macros are defined to operate on a struct timeval : #define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec) #define timercmp(tvp, uvp, cmp) ((tvp)->tv_sec cmp (uvp)->tv_sec || (tvp)->tv_sec == (uvp)->tv_sec && (tvp)->tv_usec cmp (uvp)->tv_usec) #define timerclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0) If either tv or tz is null, the corresponding structure is not set or returned. Only the super user may use settimeofday. RETURN VALUE
gettimeofday and settimeofday return 0 for success, or -1 for failure (in which case errno is set appropriately). ERRORS
EPERM settimeofday is called by someone other than the superuser. EINVAL Timezone (or something else) is invalid. EFAULT One of tv or tz pointed outside your accessible address space. NOTE
The prototype for settimeofday and the defines for timercmp, timerisset, timerclear, timeradd, timersub are (since glibc2.2.2) only avail- able if _BSD_SOURCE is defined (either explicitly, or implicitly, by not defining _POSIX_SOURCE or compiling with the -ansi flag). CONFORMING TO
SVr4, BSD 4.3 SEE ALSO
date(1), adjtimex(2), time(2), ctime(3), ftime(3) Linux 2.0.32 1997-12-10 GETTIMEOFDAY(2)
All times are GMT -4. The time now is 10:52 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy