How do i generate random numbers in loop then store it in a file every 1000000 numbers?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-2















So basically, in an infinite loop, i have to generate random numbers. I then have to create files in iteration where in 1st loop it's file1.txt, 2nd loop it's file2.txt and so on. We use the files to store every 1000000 random numbers generated, so file1.txt stores the 1st 1 million numbers, file2.txt stores the next 1 million and so on. But i think i messed up with the code. Can you help me?



int rng(){
int i;
FILE *fptr;
int k = 0;

char buffer[32];
snprintf(buffer, sizeof(char) * 32, "file%i.txt", k);
fptr=fopen(buffer, "wb");
for(i=0; i<1000000; i++)
{
fprintf(fptr, "%d", (rand() * rand()) % 1000001);
}
fclose(fptr);
k++;
}









share|improve this question




















  • 1





    Choose a language, either C or C++.

    – PaulMcKenzie
    Nov 17 '18 at 7:18











  • There is no infinite loop in this code. Every time rnd() is called, it starts over at file0.txt. And there are no delimiters being written to file between the numbers.

    – Remy Lebeau
    Nov 17 '18 at 7:21













  • @RemyLebeau can you tell me how to make it infinite?

    – odella
    Nov 17 '18 at 7:22






  • 1





    Why do you think you "messed up"? What is not working as expected? What have you tried to spot the error?

    – Nico Haase
    Nov 17 '18 at 7:26






  • 1





    If this programm really runs (virtually) endlessly, at some point in time, the int will overflow. However, as it is signed: undefined behaviour - at least for now; C++20 seems now to base signed data types on 2's complement, maybe they dropped the UB part as well; but if, you'd get files with negative name indices, which likely is not in your sense either...

    – Aconcagua
    Nov 17 '18 at 7:39




















-2















So basically, in an infinite loop, i have to generate random numbers. I then have to create files in iteration where in 1st loop it's file1.txt, 2nd loop it's file2.txt and so on. We use the files to store every 1000000 random numbers generated, so file1.txt stores the 1st 1 million numbers, file2.txt stores the next 1 million and so on. But i think i messed up with the code. Can you help me?



int rng(){
int i;
FILE *fptr;
int k = 0;

char buffer[32];
snprintf(buffer, sizeof(char) * 32, "file%i.txt", k);
fptr=fopen(buffer, "wb");
for(i=0; i<1000000; i++)
{
fprintf(fptr, "%d", (rand() * rand()) % 1000001);
}
fclose(fptr);
k++;
}









share|improve this question




















  • 1





    Choose a language, either C or C++.

    – PaulMcKenzie
    Nov 17 '18 at 7:18











  • There is no infinite loop in this code. Every time rnd() is called, it starts over at file0.txt. And there are no delimiters being written to file between the numbers.

    – Remy Lebeau
    Nov 17 '18 at 7:21













  • @RemyLebeau can you tell me how to make it infinite?

    – odella
    Nov 17 '18 at 7:22






  • 1





    Why do you think you "messed up"? What is not working as expected? What have you tried to spot the error?

    – Nico Haase
    Nov 17 '18 at 7:26






  • 1





    If this programm really runs (virtually) endlessly, at some point in time, the int will overflow. However, as it is signed: undefined behaviour - at least for now; C++20 seems now to base signed data types on 2's complement, maybe they dropped the UB part as well; but if, you'd get files with negative name indices, which likely is not in your sense either...

    – Aconcagua
    Nov 17 '18 at 7:39
















-2












-2








-2


1






So basically, in an infinite loop, i have to generate random numbers. I then have to create files in iteration where in 1st loop it's file1.txt, 2nd loop it's file2.txt and so on. We use the files to store every 1000000 random numbers generated, so file1.txt stores the 1st 1 million numbers, file2.txt stores the next 1 million and so on. But i think i messed up with the code. Can you help me?



int rng(){
int i;
FILE *fptr;
int k = 0;

char buffer[32];
snprintf(buffer, sizeof(char) * 32, "file%i.txt", k);
fptr=fopen(buffer, "wb");
for(i=0; i<1000000; i++)
{
fprintf(fptr, "%d", (rand() * rand()) % 1000001);
}
fclose(fptr);
k++;
}









share|improve this question
















So basically, in an infinite loop, i have to generate random numbers. I then have to create files in iteration where in 1st loop it's file1.txt, 2nd loop it's file2.txt and so on. We use the files to store every 1000000 random numbers generated, so file1.txt stores the 1st 1 million numbers, file2.txt stores the next 1 million and so on. But i think i messed up with the code. Can you help me?



int rng(){
int i;
FILE *fptr;
int k = 0;

char buffer[32];
snprintf(buffer, sizeof(char) * 32, "file%i.txt", k);
fptr=fopen(buffer, "wb");
for(i=0; i<1000000; i++)
{
fprintf(fptr, "%d", (rand() * rand()) % 1000001);
}
fclose(fptr);
k++;
}






c






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 '18 at 7:21







odella

















asked Nov 17 '18 at 7:15









odellaodella

61




61








  • 1





    Choose a language, either C or C++.

    – PaulMcKenzie
    Nov 17 '18 at 7:18











  • There is no infinite loop in this code. Every time rnd() is called, it starts over at file0.txt. And there are no delimiters being written to file between the numbers.

    – Remy Lebeau
    Nov 17 '18 at 7:21













  • @RemyLebeau can you tell me how to make it infinite?

    – odella
    Nov 17 '18 at 7:22






  • 1





    Why do you think you "messed up"? What is not working as expected? What have you tried to spot the error?

    – Nico Haase
    Nov 17 '18 at 7:26






  • 1





    If this programm really runs (virtually) endlessly, at some point in time, the int will overflow. However, as it is signed: undefined behaviour - at least for now; C++20 seems now to base signed data types on 2's complement, maybe they dropped the UB part as well; but if, you'd get files with negative name indices, which likely is not in your sense either...

    – Aconcagua
    Nov 17 '18 at 7:39
















  • 1





    Choose a language, either C or C++.

    – PaulMcKenzie
    Nov 17 '18 at 7:18











  • There is no infinite loop in this code. Every time rnd() is called, it starts over at file0.txt. And there are no delimiters being written to file between the numbers.

    – Remy Lebeau
    Nov 17 '18 at 7:21













  • @RemyLebeau can you tell me how to make it infinite?

    – odella
    Nov 17 '18 at 7:22






  • 1





    Why do you think you "messed up"? What is not working as expected? What have you tried to spot the error?

    – Nico Haase
    Nov 17 '18 at 7:26






  • 1





    If this programm really runs (virtually) endlessly, at some point in time, the int will overflow. However, as it is signed: undefined behaviour - at least for now; C++20 seems now to base signed data types on 2's complement, maybe they dropped the UB part as well; but if, you'd get files with negative name indices, which likely is not in your sense either...

    – Aconcagua
    Nov 17 '18 at 7:39










1




1





Choose a language, either C or C++.

– PaulMcKenzie
Nov 17 '18 at 7:18





Choose a language, either C or C++.

– PaulMcKenzie
Nov 17 '18 at 7:18













There is no infinite loop in this code. Every time rnd() is called, it starts over at file0.txt. And there are no delimiters being written to file between the numbers.

– Remy Lebeau
Nov 17 '18 at 7:21







There is no infinite loop in this code. Every time rnd() is called, it starts over at file0.txt. And there are no delimiters being written to file between the numbers.

– Remy Lebeau
Nov 17 '18 at 7:21















@RemyLebeau can you tell me how to make it infinite?

– odella
Nov 17 '18 at 7:22





@RemyLebeau can you tell me how to make it infinite?

– odella
Nov 17 '18 at 7:22




1




1





Why do you think you "messed up"? What is not working as expected? What have you tried to spot the error?

– Nico Haase
Nov 17 '18 at 7:26





Why do you think you "messed up"? What is not working as expected? What have you tried to spot the error?

– Nico Haase
Nov 17 '18 at 7:26




1




1





If this programm really runs (virtually) endlessly, at some point in time, the int will overflow. However, as it is signed: undefined behaviour - at least for now; C++20 seems now to base signed data types on 2's complement, maybe they dropped the UB part as well; but if, you'd get files with negative name indices, which likely is not in your sense either...

– Aconcagua
Nov 17 '18 at 7:39







If this programm really runs (virtually) endlessly, at some point in time, the int will overflow. However, as it is signed: undefined behaviour - at least for now; C++20 seems now to base signed data types on 2's complement, maybe they dropped the UB part as well; but if, you'd get files with negative name indices, which likely is not in your sense either...

– Aconcagua
Nov 17 '18 at 7:39














1 Answer
1






active

oldest

votes


















0














You can make an endless loop using while(1) and then use a simple counter to count the number of elements in the current file.



Like:



#include <stdio.h>

#define ELEMENTS_PER_FILE 5

int main(void) {
unsigned k = 0;
unsigned file_number = 1;

printf("Open file%u.txtn", file_number);

while(1)
{
if (k == ELEMENTS_PER_FILE)
{
++file_number;
printf("Open file%u.txtn", file_number);
k = 0;
}

++k;
printf(" Add random number #%u to filen", k);

}

return 0;
}


Output:



Open file1.txt
Add random number #1 to file
Add random number #2 to file
Add random number #3 to file
Add random number #4 to file
Add random number #5 to file
Open file2.txt
Add random number #1 to file
Add random number #2 to file
Add random number #3 to file
Add random number #4 to file
Add random number #5 to file
Open file3.txt
Add random number #1 to file
Add random number #2 to file
. . .


All you need to do is to replace the printf with the code doing what the print says.



Using a function to generate a single file, you can organize the code a little better. Like:



#include <stdio.h>

#define ELEMENTS_PER_FILE 5
#define FILES_TO_GENERATE 3

void make_file(unsigned num)
{
int i;
printf("Open file%u.txtn", num);
for (i=1; i <= ELEMENTS_PER_FILE; ++i)
{
printf(" Add random number #%d to filen", i);
}
printf("Close file%u.txtn", num);
}

int main(void) {
unsigned file_number = 1;

while(file_number <= FILES_TO_GENERATE) // or while(1) if you really want infinite
{
make_file(file_number);
++file_number;
}

return 0;
}


Output:



Open file1.txt
Add random number #1 to file
Add random number #2 to file
Add random number #3 to file
Add random number #4 to file
Add random number #5 to file
Close file1.txt
Open file2.txt
Add random number #1 to file
Add random number #2 to file
Add random number #3 to file
Add random number #4 to file
Add random number #5 to file
Close file2.txt
Open file3.txt
Add random number #1 to file
Add random number #2 to file
Add random number #3 to file
Add random number #4 to file
Add random number #5 to file
Close file3.txt





share|improve this answer


























  • I'd rather name it ELEMENTS_PER_FILE...

    – Aconcagua
    Nov 17 '18 at 8:11














Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53349083%2fhow-do-i-generate-random-numbers-in-loop-then-store-it-in-a-file-every-1000000-n%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You can make an endless loop using while(1) and then use a simple counter to count the number of elements in the current file.



Like:



#include <stdio.h>

#define ELEMENTS_PER_FILE 5

int main(void) {
unsigned k = 0;
unsigned file_number = 1;

printf("Open file%u.txtn", file_number);

while(1)
{
if (k == ELEMENTS_PER_FILE)
{
++file_number;
printf("Open file%u.txtn", file_number);
k = 0;
}

++k;
printf(" Add random number #%u to filen", k);

}

return 0;
}


Output:



Open file1.txt
Add random number #1 to file
Add random number #2 to file
Add random number #3 to file
Add random number #4 to file
Add random number #5 to file
Open file2.txt
Add random number #1 to file
Add random number #2 to file
Add random number #3 to file
Add random number #4 to file
Add random number #5 to file
Open file3.txt
Add random number #1 to file
Add random number #2 to file
. . .


All you need to do is to replace the printf with the code doing what the print says.



Using a function to generate a single file, you can organize the code a little better. Like:



#include <stdio.h>

#define ELEMENTS_PER_FILE 5
#define FILES_TO_GENERATE 3

void make_file(unsigned num)
{
int i;
printf("Open file%u.txtn", num);
for (i=1; i <= ELEMENTS_PER_FILE; ++i)
{
printf(" Add random number #%d to filen", i);
}
printf("Close file%u.txtn", num);
}

int main(void) {
unsigned file_number = 1;

while(file_number <= FILES_TO_GENERATE) // or while(1) if you really want infinite
{
make_file(file_number);
++file_number;
}

return 0;
}


Output:



Open file1.txt
Add random number #1 to file
Add random number #2 to file
Add random number #3 to file
Add random number #4 to file
Add random number #5 to file
Close file1.txt
Open file2.txt
Add random number #1 to file
Add random number #2 to file
Add random number #3 to file
Add random number #4 to file
Add random number #5 to file
Close file2.txt
Open file3.txt
Add random number #1 to file
Add random number #2 to file
Add random number #3 to file
Add random number #4 to file
Add random number #5 to file
Close file3.txt





share|improve this answer


























  • I'd rather name it ELEMENTS_PER_FILE...

    – Aconcagua
    Nov 17 '18 at 8:11


















0














You can make an endless loop using while(1) and then use a simple counter to count the number of elements in the current file.



Like:



#include <stdio.h>

#define ELEMENTS_PER_FILE 5

int main(void) {
unsigned k = 0;
unsigned file_number = 1;

printf("Open file%u.txtn", file_number);

while(1)
{
if (k == ELEMENTS_PER_FILE)
{
++file_number;
printf("Open file%u.txtn", file_number);
k = 0;
}

++k;
printf(" Add random number #%u to filen", k);

}

return 0;
}


Output:



Open file1.txt
Add random number #1 to file
Add random number #2 to file
Add random number #3 to file
Add random number #4 to file
Add random number #5 to file
Open file2.txt
Add random number #1 to file
Add random number #2 to file
Add random number #3 to file
Add random number #4 to file
Add random number #5 to file
Open file3.txt
Add random number #1 to file
Add random number #2 to file
. . .


All you need to do is to replace the printf with the code doing what the print says.



Using a function to generate a single file, you can organize the code a little better. Like:



#include <stdio.h>

#define ELEMENTS_PER_FILE 5
#define FILES_TO_GENERATE 3

void make_file(unsigned num)
{
int i;
printf("Open file%u.txtn", num);
for (i=1; i <= ELEMENTS_PER_FILE; ++i)
{
printf(" Add random number #%d to filen", i);
}
printf("Close file%u.txtn", num);
}

int main(void) {
unsigned file_number = 1;

while(file_number <= FILES_TO_GENERATE) // or while(1) if you really want infinite
{
make_file(file_number);
++file_number;
}

return 0;
}


Output:



Open file1.txt
Add random number #1 to file
Add random number #2 to file
Add random number #3 to file
Add random number #4 to file
Add random number #5 to file
Close file1.txt
Open file2.txt
Add random number #1 to file
Add random number #2 to file
Add random number #3 to file
Add random number #4 to file
Add random number #5 to file
Close file2.txt
Open file3.txt
Add random number #1 to file
Add random number #2 to file
Add random number #3 to file
Add random number #4 to file
Add random number #5 to file
Close file3.txt





share|improve this answer


























  • I'd rather name it ELEMENTS_PER_FILE...

    – Aconcagua
    Nov 17 '18 at 8:11
















0












0








0







You can make an endless loop using while(1) and then use a simple counter to count the number of elements in the current file.



Like:



#include <stdio.h>

#define ELEMENTS_PER_FILE 5

int main(void) {
unsigned k = 0;
unsigned file_number = 1;

printf("Open file%u.txtn", file_number);

while(1)
{
if (k == ELEMENTS_PER_FILE)
{
++file_number;
printf("Open file%u.txtn", file_number);
k = 0;
}

++k;
printf(" Add random number #%u to filen", k);

}

return 0;
}


Output:



Open file1.txt
Add random number #1 to file
Add random number #2 to file
Add random number #3 to file
Add random number #4 to file
Add random number #5 to file
Open file2.txt
Add random number #1 to file
Add random number #2 to file
Add random number #3 to file
Add random number #4 to file
Add random number #5 to file
Open file3.txt
Add random number #1 to file
Add random number #2 to file
. . .


All you need to do is to replace the printf with the code doing what the print says.



Using a function to generate a single file, you can organize the code a little better. Like:



#include <stdio.h>

#define ELEMENTS_PER_FILE 5
#define FILES_TO_GENERATE 3

void make_file(unsigned num)
{
int i;
printf("Open file%u.txtn", num);
for (i=1; i <= ELEMENTS_PER_FILE; ++i)
{
printf(" Add random number #%d to filen", i);
}
printf("Close file%u.txtn", num);
}

int main(void) {
unsigned file_number = 1;

while(file_number <= FILES_TO_GENERATE) // or while(1) if you really want infinite
{
make_file(file_number);
++file_number;
}

return 0;
}


Output:



Open file1.txt
Add random number #1 to file
Add random number #2 to file
Add random number #3 to file
Add random number #4 to file
Add random number #5 to file
Close file1.txt
Open file2.txt
Add random number #1 to file
Add random number #2 to file
Add random number #3 to file
Add random number #4 to file
Add random number #5 to file
Close file2.txt
Open file3.txt
Add random number #1 to file
Add random number #2 to file
Add random number #3 to file
Add random number #4 to file
Add random number #5 to file
Close file3.txt





share|improve this answer















You can make an endless loop using while(1) and then use a simple counter to count the number of elements in the current file.



Like:



#include <stdio.h>

#define ELEMENTS_PER_FILE 5

int main(void) {
unsigned k = 0;
unsigned file_number = 1;

printf("Open file%u.txtn", file_number);

while(1)
{
if (k == ELEMENTS_PER_FILE)
{
++file_number;
printf("Open file%u.txtn", file_number);
k = 0;
}

++k;
printf(" Add random number #%u to filen", k);

}

return 0;
}


Output:



Open file1.txt
Add random number #1 to file
Add random number #2 to file
Add random number #3 to file
Add random number #4 to file
Add random number #5 to file
Open file2.txt
Add random number #1 to file
Add random number #2 to file
Add random number #3 to file
Add random number #4 to file
Add random number #5 to file
Open file3.txt
Add random number #1 to file
Add random number #2 to file
. . .


All you need to do is to replace the printf with the code doing what the print says.



Using a function to generate a single file, you can organize the code a little better. Like:



#include <stdio.h>

#define ELEMENTS_PER_FILE 5
#define FILES_TO_GENERATE 3

void make_file(unsigned num)
{
int i;
printf("Open file%u.txtn", num);
for (i=1; i <= ELEMENTS_PER_FILE; ++i)
{
printf(" Add random number #%d to filen", i);
}
printf("Close file%u.txtn", num);
}

int main(void) {
unsigned file_number = 1;

while(file_number <= FILES_TO_GENERATE) // or while(1) if you really want infinite
{
make_file(file_number);
++file_number;
}

return 0;
}


Output:



Open file1.txt
Add random number #1 to file
Add random number #2 to file
Add random number #3 to file
Add random number #4 to file
Add random number #5 to file
Close file1.txt
Open file2.txt
Add random number #1 to file
Add random number #2 to file
Add random number #3 to file
Add random number #4 to file
Add random number #5 to file
Close file2.txt
Open file3.txt
Add random number #1 to file
Add random number #2 to file
Add random number #3 to file
Add random number #4 to file
Add random number #5 to file
Close file3.txt






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 17 '18 at 8:14

























answered Nov 17 '18 at 7:46









43864274386427

22.2k31846




22.2k31846













  • I'd rather name it ELEMENTS_PER_FILE...

    – Aconcagua
    Nov 17 '18 at 8:11





















  • I'd rather name it ELEMENTS_PER_FILE...

    – Aconcagua
    Nov 17 '18 at 8:11



















I'd rather name it ELEMENTS_PER_FILE...

– Aconcagua
Nov 17 '18 at 8:11







I'd rather name it ELEMENTS_PER_FILE...

– Aconcagua
Nov 17 '18 at 8:11






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53349083%2fhow-do-i-generate-random-numbers-in-loop-then-store-it-in-a-file-every-1000000-n%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python