Sponsored Content
Top Forums Programming C Help; generating a random number. Post 302360583 by Zykl0n-B on Friday 9th of October 2009 11:08:02 AM
Old 10-09-2009
As Corona688 says,
If you use the same value to generate a "random" value, the algorithm is always going to perform the same operation with the same number, so, you need to parse values which are always going to be different, and yes, with gettimeofday() you can parse system time in nanoseconds to get a different number every nanosecond.

Try this:

Code:
#include <stdio.h>
#include <time.h>
#include <sys/time.h>

int main(void){
int i;
unsigned int value;
struct timeval tv;
struct timezone tz;
struct tm *tm;
tm = localtime(&tv.tv_sec);

for(i = 0;i < 30; i++){
gettimeofday(&tv, &tz);
value = rand_r(&tv.tv_usec);
printf("%i\n", value);
}
return(0);
}

Zykl0n-B
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

generating random numbers with hamming distance 4

Hi I want to genrate 10 random 32 bit binary numbers with hamming distance 4 and 8. 11010110010101010101010101010101 11010110010101010100010101010010 if we look carefully at these two binary numbers they differ at 4 places hence hamming distance 4. Now I want to genrate these numbers... (2 Replies)
Discussion started by: hack_tom
2 Replies

2. Shell Programming and Scripting

Generating random number within a specific range (0.5-1.5)

Hello, need a way to generate numbers within 0.5-1.5 range Has to be totally random: 0.6 1.1 0.8 1.5 0.6 and so on.... How to? (10 Replies)
Discussion started by: TehOne
10 Replies

3. Shell Programming and Scripting

Generating random numbers

Hi, I am having trouble with generating random numbers. can this be done with awk? So I have a file that looks like this: 23 30 24 40 26 34 So column1 is start and column2 is end. I want to generate 3 random #'s between start and stop: So the output will look like this: ... (9 Replies)
Discussion started by: phil_heath
9 Replies

4. Programming

generating 16 digit random number in C

Hi, How can we generate 16 digit random nos in C. (10 Replies)
Discussion started by: ajaysahoo
10 Replies

5. Programming

Generating Random Number in Child Process using Fork

Hello All, I am stuck up in a program where the rand functions ends up giving all the same integers. Tried sleep, but the numbers turned out to be same... Can anyone help me out how to fix this issue ? I have called the srand once in the program, but I feel like when I call fork the child process... (5 Replies)
Discussion started by: manisum
5 Replies

6. UNIX for Dummies Questions & Answers

Generating 512MB file with dd using random data

Hello. Could anyone help me with my little annoying problem? I have to generate a 512 MB file made up with random data using DD. After some internet digging I found out that the command is: dd if=/dev/urandom of=/exemple/file bs=512MB After running this command the... (2 Replies)
Discussion started by: razolo13
2 Replies

7. Shell Programming and Scripting

Generating Random Number in certain range

Hi there I am trying to generate a random number between 40 and 70 using the shell here is my code so far and it keeps going above 70. all help much appreciated! comp=$(( RANDOM%70+40 )) echo $comp (4 Replies)
Discussion started by: faintingquiche
4 Replies

8. Shell Programming and Scripting

Random number generating script?

Having a hard time with this. Very new to scripting and linux. Spent all sunday trying to do this. Appreciate some help and maybe help breaking down what the syntax does. Create a Bash program. It should have the following properties • Creates a secret number between 1 and 100 i. The... (3 Replies)
Discussion started by: LINUXnoob15
3 Replies

9. Shell Programming and Scripting

Generating a Random String of 'n' length

Hi, How can I generate a string of random characters (alpha+numeric) of a particular length ? For e.g. for n=5, output = 'kasjf' n=10, output = 'hedbcd902k' Also, please let me know if random (valid) dates could also be generated. Thanks (7 Replies)
Discussion started by: rishigc
7 Replies

10. 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
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 06:19 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy