LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-21-2008, 05:05 PM   #1
ssaslam
LQ Newbie
 
Registered: Feb 2008
Posts: 3

Rep: Reputation: 0
Generate random numbers in C program


Hi, im a newbie here...
Can any one show me a sample program for generating random number like 1,4,7,10,13,16,19........
i need it in c program i'm trying this but it just produces a random number... i need it in a sequence... so please help!!!!!!!
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int
main (int argc, char *argv[])
{
/* Simple "srand()" seed: just use "time()" */
unsigned int iseed = (unsigned int)time(NULL);
srand (iseed);

/* Now generate 5 pseudo-random numbers */
int i;
for (i=0; i<5; i++)
{
printf ("rand[%d]= %u\n",
i, rand ());
system ("PAUSE");
}
return 0;
}
 
Old 02-21-2008, 05:08 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Can you explain a bit more. That prog produces rand nums ok; what do you mean you want them in a 'sequence' ?
 
Old 02-21-2008, 05:14 PM   #3
rg.viza
Member
 
Registered: Aug 2006
Posts: 74

Rep: Reputation: 15
Quote:
Originally Posted by ssaslam View Post
Hi, im a newbie here...
Can any one show me a sample program for generating random number like 1,4,7,10,13,16,19........
i need it in c program i'm trying this but it just produces a random number... i need it in a sequence... so please help!!!!!!!
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int
main (int argc, char *argv[])
{
/* Simple "srand()" seed: just use "time()" */
unsigned int iseed = (unsigned int)time(NULL);
srand (iseed);

/* Now generate 5 pseudo-random numbers */
int i;
for (i=0; i<5; i++)
{
printf ("rand[%d]= %u\n",
i, rand ());
system ("PAUSE");
}
return 0;
}
use the numbers to populate an array, then sort the array. Figure out how to code this and you've learned something.

Edit: this is really a question about programming in C.
-Viz

Last edited by rg.viza; 02-21-2008 at 05:16 PM.
 
Old 02-21-2008, 05:19 PM   #4
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,908

Rep: Reputation: 356Reputation: 356Reputation: 356Reputation: 356
Quote:
Can any one show me a sample program for generating random number like 1,4,7,10,13,16,19.....
This is not a set of random numbers; this is a sequence of numbers which begins with the number 1, adds 3 to the first number, adds 3 to the to the second sum, etc. What's so difficult about that?

I mean, really, it isn't even as difficult at a Fibonacci series, which isn't all that difficult. But it is a bit more difficult than a simple series incremented by three at each increase.

number=1 to start the series

followed by

number+=3 to continue the series

Last edited by bigrigdriver; 02-21-2008 at 05:22 PM.
 
Old 02-21-2008, 09:51 PM   #5
ssaslam
LQ Newbie
 
Registered: Feb 2008
Posts: 3

Original Poster
Rep: Reputation: 0
wut was i thinking...

i meant real numbers....
like 6 places
all i want it to show is 1,4,7,10,and 13

sry abt the mis...
 
Old 10-23-2008, 08:28 PM   #6
It_Linux
LQ Newbie
 
Registered: Oct 2008
Posts: 1

Rep: Reputation: 0
Thanks a lot

Thasnks for your answers. GOD Bless you.
 
  


Reply

Tags
program



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
using /dev/random to output random numbers on a text file guguma Programming 4 04-02-2007 01:42 PM
Generate random number with C++ TruongAn Programming 5 11-09-2005 12:01 AM
can bash generate random digit? sorno Linux - Newbie 5 08-17-2005 06:05 AM
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 > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:04 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