LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 11-08-2004, 10:55 PM   #1
blufire
Member
 
Registered: Jul 2004
Location: Japan
Distribution: Fedora
Posts: 148

Rep: Reputation: 16
Random number generators


I am looking for a tutorial to teach me how to write a random number generator in c. I went online and I downloaded a generator but would like to be able to program my own. Are there any tutorials out there or can someone give me a step by step of a simple random number generator I could work with to help me learn. Thankyou
 
Old 11-08-2004, 11:01 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
This site might be helpful for you:
http://random.mat.sbg.ac.at/
 
Old 11-08-2004, 11:31 PM   #3
sheds
Member
 
Registered: Oct 2004
Location: Costa Rica
Distribution: Mandrake 9.1
Posts: 30

Rep: Reputation: 15
I have an example right here. I just did it. Maybe it's what you're looking for. It generates numbers between 0 and 10, and writes them to a file.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int writetoFile(int intArray[5]);

int main(void)
{
int anotherIntArray[6];
int i;

/*enter data*/
printf("Entering data (5 lines):\n");
srand((unsigned int)time(NULL));
int j;
for(j=0;j<5;j++)
{
anotherIntArray[j]=rand()%10;
printf("%d,",anotherIntArray[j]);
}
anotherIntArray[5]=0;

/*writing to file*/
writetoFile(anotherIntArray);
printf("\nDone writing to file...%d\n",anotherIntArray[5]);
return 0;
}

int writetoFile(int intArray[5])
{
/*open file and write to it*/
FILE *openedFile;
openedFile=fopen("data.dat","w");
if(openedFile==NULL)
{
printf("Could not open file. Terminate program");
exit(0);
}
int i;
for(i=0;i<5;i++)
{
printf("\nWriting to file %d",intArray[i]);
fprintf(openedFile,"%d\n",intArray[i]);
}
fclose(openedFile);
return 0;
}
 
Old 11-08-2004, 11:48 PM   #4
sheds
Member
 
Registered: Oct 2004
Location: Costa Rica
Distribution: Mandrake 9.1
Posts: 30

Rep: Reputation: 15
How do you post some code in the boxes that look like images? Cause i just dragged that code from KWrite but the indentation went away.
 
Old 11-08-2004, 11:55 PM   #5
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
Quote:
Originally posted by sheds
How do you post some code in the boxes that look like images? Cause i just dragged that code from KWrite but the indentation went away.
Use the code tag in the vb Code.

Code:
Type some code here

void main()
{
       // indented ....
}
 
Old 11-09-2004, 06:02 PM   #6
blufire
Member
 
Registered: Jul 2004
Location: Japan
Distribution: Fedora
Posts: 148

Original Poster
Rep: Reputation: 16
Thankyou all. On the first site I am learning about the generators and how they work. The one written out here is great for me to practice with. Thankyou again
 
  


Reply



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
Generate random number with C++ TruongAn Programming 5 11-09-2005 12:01 AM
random number generation sailu_mvn Programming 4 03-10-2005 02:10 PM
Random Number Game tearinox Programming 4 08-20-2004 10:12 PM
Python Random number sethgeekx86 Programming 4 06-10-2004 08:55 AM
I need random number in C ... purpleburple Programming 4 10-28-2002 04:37 AM

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

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