LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-14-2009, 06:50 AM   #1
lucky.shubham
LQ Newbie
 
Registered: Oct 2009
Posts: 7

Rep: Reputation: 0
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
 
Old 10-15-2009, 12:28 AM   #2
greeklegend
Member
 
Registered: Feb 2006
Location: At a computer
Distribution: Ubuntu 7.04, LFS 6.3 rc1 (living dangerously ;), Windows XP
Posts: 75

Rep: Reputation: 15
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;
}
 
Old 10-17-2009, 12:12 AM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Moved: This thread is more suitable in Programming and has been moved accordingly to help your question get the exposure it deserves.
 
Old 10-21-2009, 05:41 AM   #4
lucky.shubham
LQ Newbie
 
Registered: Oct 2009
Posts: 7

Original Poster
Rep: Reputation: 0
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?
 
Old 10-21-2009, 08:16 AM   #5
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
Something like
Code:
while (your_condition)
{
    sprintf(sendline,"%d",your_randomnumber);
    ....
    ....
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
generating random numbers in C abk4523 Programming 20 01-10-2011 04:08 PM
using /dev/random to output random numbers on a text file guguma Programming 4 04-02-2007 01:42 PM
Generating Gaussian Random Numbers R00ts Programming 2 08-10-2004 11:51 PM
Creating random numbers from shell with /dev/random khermans Linux - General 1 07-13-2004 12:12 PM
Generating random numbers for C program J-Stew Programming 14 02-06-2004 04:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration