LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   generating random numbers for 10 minutes (https://www.linuxquestions.org/questions/programming-9/generating-random-numbers-for-10-minutes-761803/)

lucky.shubham 10-14-2009 06:50 AM

generating random numbers for 10 minutes
 
i have to generate traffic over a wireless network by sending random numbers over the network for about,say 10 minutes.What i dont understand is how to time the generation of random numbers to a particular value.
I am doing the programming in C and trying to use the rand() function.

Cheers,
Shubham

greeklegend 10-15-2009 12:28 AM

I don't quite understand what you mean by "how to time the generation of random numbers to a particular value". Do you mean, how to loop over the random-number-sending bit for 10 minutes? Something like this would work...

Code:

#include <sys/types.h>
#include <time.h>

int main()
{
int num_seconds = 600;
time_t starttime;
time_t endtime;

time(&starttime);
time(&endtime);
while(endtime <= starttime + num_seconds)
{
//do your numbersending here
time(&endtime);
}
return 0;
}


jschiwal 10-17-2009 12:12 AM

Moved: This thread is more suitable in Programming and has been moved accordingly to help your question get the exposure it deserves.

lucky.shubham 10-21-2009 05:41 AM

hey...
thnx for the code...
there's another problem that i'm facing with the sending of random numbers to generate traffic,which is,where do i edit the program for the client.
I'm using the following program for the client :

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <netdb.h>
#include <stdlib.h>
#include <errno.h>
#include <readline/readline.h>
#include <readline/history.h>
int main(int argc, char **argv)
{
int sockfd;
struct sockaddr_in servaddr;
if (argc != 2){
printf("usage: tcpcli <IPaddress>");
exit(0);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(1200);
inet_pton(AF_INET, argv[1], &servaddr.sin_addr);
connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
str_cli(stdin, sockfd); /* do it all */
exit(0);
}
str_cli(FILE *fp, int sockfd)
{
char sendline[1000], recvline[1000];
while (fgets(sendline, 1000, fp) != NULL) {
write(sockfd, sendline, strlen (sendline));
if (read(sockfd, recvline,1000) == 0){
printf("str_cli: server terminated prematurely");
exit(0);
}
fputs(recvline, stdout);
return(0);
}
}

this program uses "fgets", which asks for input from the user.but i want to use rand() to generate random numbers and send them over to the server. how do i do that?

Wim Sturkenboom 10-21-2009 08:16 AM

Something like
Code:

while (your_condition)
{
    sprintf(sendline,"%d",your_randomnumber);
    ....
    ....
}



All times are GMT -5. The time now is 02:24 AM.