Why do we use 'NULL'? [duplicate] - c

This question already has answers here:
What is time(NULL) in C?
(8 answers)
Closed 3 years ago.
Why do we use 'NULL' in code below.
Why can't we multiply the seed by an integer?
Sorry I am new to C++.
CODE
srand(time(NULL));

The time function can write the time to a location provided by a pointer to the function call. This pointer argument can be a null pointer, and then time only returns the current time.
On most systems the time function returns the number of seconds since an epoch, and as such is a pretty unique integer value to use for seeding the random number generator.
The single statement
srand(time(NULL));
is equivalent to
time_t temp_time = time(NULL);
srand(temp_time);
Or if we want to use a non-null pointer
time_t temp_time;
time(&temp_time);
srand(temp_time);

Related

How to create random number values in c [duplicate]

This question already has answers here:
Recommended way to initialize srand?
(15 answers)
srand() — why call it only once?
(7 answers)
Closed 2 years ago.
i am trying to create random data so i can review an algorithm in cache policy. When i use the first code with rand() i get the same numbers every time i run my code(i knew this was coming) so i tried to use srand. when i inserted it i get different numbers every time i run the code but the same 10 numbers at my loop. What am i doing wrong to create random data??? After that i will make with zip ununiformed data.
sorry my bad the loop is in main function:
for(counter=0; counter<10; counter++){
printf("Number %d is %lf\n", counter, rand_val());
}
And i am using this function to create numbers.
double rand_val(){
double rnum;
srand(time(NULL));
rnum=rand();
return(rnum);
}

Reading system time as integers? [duplicate]

This question already has answers here:
Get the current time in C [duplicate]
(11 answers)
Closed 5 years ago.
I'm doing a C programming course at uni and one of the tasks is to create an OpenGL analogue clock on the screen. I want to drive the hour, minute, and second hands from the actual time.
How do I read the hour, minute, and seconds from system time? As integers would be best. I've had a look around and can't find anything quite like what I'm after.
You should use localtime, like this:
#include <time.h>
time_t timestamp = time(NULL);
if (timestamp != (time_t)-1) {
struct tm *t = localtime(&timestamp);
// Use t->tm_hour, t->tm_min and t->tm_sec, which are ints
}
See man 3 localtime for more info.

How to retrieve specific index value from web_reg_save_param arrray [duplicate]

This question already has answers here:
LoadRunner web_reg_save_param, ord=all, paramName_count issues
(4 answers)
Closed 6 years ago.
I am using the web param function to retreive certain values. I want to get the value of a specific index from the array item and store it in a parameter to be used in a web_link call.
char * tempVal;
web_reg_save_param("dynArray","LB=/EmployeeProfile/","RB=\">","ORD=ALL",LAST);
tempVal = "{dynArray_2}";
There is no error for the above statements but when accessing tempVal it is giving error
vuser_init.c(143): web_link("emp") started [MsgId: MMSG-26355]
vuser_init.c(143): Warning: The string 'tempVal' with parameter delimiters is not a parameter.
vuser_init.c(143): Error -27995: Requested link ("Text={tempVal}") not found [MsgId: MERR-27995]
You have a problem which is C language based. You cannot simply equate two items as you have done.
take a look at the strcpy() function combined with lr_eval_string()
strcpy (destination_C_variable, lr_eval_string( "{LoadRunner_source_variable}" ) );

Function call inside a loop in C [duplicate]

This question already has answers here:
C program - srand() [duplicate]
(6 answers)
Closed 7 years ago.
int random(){//function for random number generation
time_t t;
srand((unsigned) time(&t));
int rand = rand()%468;
return rand;
}
I want to put this function in a loop so it assigns a random number to a variable during each iteration of the loop, so i figured i would try this:
for(x=0;x<n;x++){
y=random();
printf("%d\n",y);
}
However this doesn't work as i expected, y just gets the number from the first iteration and repeats it, can someone explain how i can force the function to recall so it will set a new value to y?
You should not call srand() everytime before calling rand().
The way you are calling srand() always passes the same value as the seed, because the difference in seconds between each call is 0, since it's a lot smaller than 1s.
Since you always call srand() with the same seed and right before rand(), you will get always the same value from rand().
Just call srand() once at the start of the program, so in every run there will be different random numbers generated.

Creating a random array of Bits [duplicate]

This question already has answers here:
Why do I always get the same sequence of random numbers with rand()?
(12 answers)
Closed 7 years ago.
Using rand(), I'm trying to create an array which generates 32 random numbers each time I run the program. However my program below gives me the same 32 random bits each time. Is there anyway I can get it to generate different 32 bits each time I run the program?
for(a=0;a<32;a++)
{
ran[a]= (rand()%2);
}
You need to set up the random seed each time you run the program to something different to do that. What people do is usually this:
#include <time.h> /* time */
...
srand (time(NULL));
...
//Calls to rand();
Then your random seed is different at every start of the program. Check more details about srand here.

Resources