Hey, I'm hoping someone can help out here. I'm trying to use the rand() function to generate sequential random numbers in C, and then write them to file. Now I know it's possible, I'm just not sure whether it is the way I want to do it. I have already used the rand function to generate numbers and write them to file, it's just the doing it in sequence that's being a big pain.

Now at the mo I've got this code:

```
#include <stdio.h>
#include <stdlib.h>
FILE *bptr; /* Pointer to file 'bptr' */
int i, j, k, n;
int main(void)
{
printf("\nOpening file bptr.txt for reading...\n");
if ((bptr = fopen("bptr.txt", "w")) == NULL)
{
printf("\nI won't open file cos there's an error\n");
exit(1);
}
else
{
printf("\nI've opened file cos you asked for it.\n");
}
/* For loop to generate 50 random numbers*/
for (i = 0; i < 50; ++i)
{
if (i % 14 == 0)
putchar('\n');
j = rand();
k = j; /* Assign random number generated to a variable*/
if (k > j) /* Attempt to check if random number generated is larger than previous, if it is then it gets printed to file, else it gets printed to screen */
{
fprintf(bptr, "\n%d\t", k);
}
else
printf("%7d\t", k);
/*if (i < j)
fprintf(bptr, "\n%d\t", j);
//printf("%7d", j);*/
}
/*for ( ; i < j; ++i)
{
j = k;
}*/
printf("\n");
printf("\nClosing text file...\n");
fclose(bptr);
return 0;
}
```

All it gives me is 50 integers printed out to the screen, and not even one printed to the file. What I am trying to do is to let rand run, and generate as many numbers as it needs to. Then if the number generated is larger than the previous number, then it should get printed to file, and wait for the next number generated by rand that is larger than it. Any ideas?

Cheers

The test in line 29 can never ever be true because the line just before it set k equal to j. Move line 27 to after line 34 and see if that helps.

Thanks Dragon, I did that, and I got 26 of the 50 numbers printed out on the screen, and the remaining 24 printed out to the file. The first number printed to the screen is '0', but all the numbers are random, no increasing sequence like I want.

Is there another way I can rework the control statements to print the numbers in order?

>>print the numbers in order?

First you need to specify what order you are going to print them in and then where you are going to print them. If you want to print them to file in ascending value then you will need to store them all in a container within the program and sort them before printing them to the file. There are several containers you can use, though arrays are commonly used by beginners, and there are several sorting protocols, though bubble sort is commonly used by beginners.

It's another ballgame completely if you want to print them to the screen in the order they were generated.

In addition, generally speaking, you want to seed the random number generating routine with some value before you call rand(), otherwise you may well get the same random sequence each time you run the program. You only need to seed the random number generating routine once in your program however.

Thanks Lerner,

I DO want to have the same random sequence each time I run the program so I think for now I don't want to seed rand(). And I do know a bit about sorting, but I'm not allowed to use it for this problem.

So basically, I want to generate random numbers, but then write them to a file or the screen in an increasing sequence, which means that I want to control rand a bit... Maybe have a temporary variable or something.

Actually, my idea is to run rand (not sure how many times I'll need to), and to then say for each time it returns a number, the program should check that current number with the previous one; if the current number is larger than the previous one, it gets printed to screen, if not, it gets ignored (i.e. nothing happens to it) and rand keeps going. And I'm finding that quite challenging at the moment.

>if the current number is larger than the previous one, it gets printed to screen, if not, it gets ignored

What happens if the current number is larger that the previous one, but

smaller than the one before the previous one? How would you know that?

Actually, my idea is to run rand (not sure how many times I'll need to), and to then say for each time it returns a number, the program should check that current number with the previous one; if the current number is larger than the previous one, it gets printed to screen, if not, it gets ignored (i.e. nothing happens to it) and rand keeps going. And I'm finding that quite challenging at the moment.

rand() could return a large value (RAND_MAX for instance); your program would then loop infinitely. you will have to do something like

if the value of ( RAND_MAX - largest_so_far ) is less than random_numbers_remaining, start all over again from scratch.

if you are willing to iterate from 0 to RAND_MAX, the problem is much easier to solve.

added later

```
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std ;
void generate_ascending_rand( int N )
{
int available = RAND_MAX ;
int required = N ;
for( int i=0 ; i<available ; ++i )
// to choose required out of the remaining (available-i)
if( ( rand() % (available-i) ) < required )
{
--required ;
cout << i << '\n' ;
}
}
int main()
{
srand( unsigned( time(0)) ) ;
generate_ascending_rand(50) ;
}
```

What happens if the current number is larger that the previous one, but

smaller than the one before the previous one? How would you know that?

Hey Aia, I guess I am assuming that if I start from zero, then I can keep generating random numbers, telling the program which ones I want to keep & print to file i.e. the one that is larger than the previous one written to file; and then just keep looping that?:-/

Hey vijayan is that C++, because I'm using C..?

I guess I can try to convert most of it though, afterall it's only an algorithm, shouldn't be too hard right??

*After typing out code...*

Err, sorry, but what is meant to be happening in lines 15 & 16 of the code you put vijayan? I've got this so far

```
#include <stdio.h>
#include <stdlib.h>
void generate_ascending_number(int n)
{
int available = RAND_MAX;
int required = n;
for (int i=0; i<available; ++i)
if ( (rand() % (available-i)) < required)
{
}
}
int main()
{
generate_ascending_number(50);
return 0;
}
```

converting it to C is trivial; as you said it is only an algorithm

```
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void generate_ascending_rand( int N )
{
int available = RAND_MAX ;
int required = N ;
int i = 0 ;
for( ; i<available ; ++i )
if( ( rand() % (available-i) ) < required )
{
--required ;
printf( "%d\n", i ) ;
}
}
int main()
{
srand( (unsigned)time(NULL) ) ;
generate_ascending_rand(50) ;
return 0 ; // required in C
}
```

OK, I'm being silly, this is the correct version of the code that compiles and runs.

```
#include <stdio.h>
#include <stdlib.h>
int i;
void generate_ascending_number(int n)
{
int available = RAND_MAX;
int required = n;
for (i=0; i<available; ++i)
{
if ( (rand() % (available-i)) < required)
{
printf("%d\t", i);
}
}
}
int main()
{
generate_ascending_number(20);
return 0;
}
```

Thanks very much vijayan! Now when it runs I get 176 numbers printed out in sequence... How can I get more control over how many it prints out, because I'll be wanting to do up to 10,000, and possibly more? Is it by playing around with line 12?

the parameter passed to the function is the number of ascending random integers to be printed.

modify line 24 to

generate_ascending_number(1024);

to print 1024 numbers.

Hmmm. I've put 1024 in there, and it sure prints out a lot of numbers, which might well be 1024 numbers. But when I put in 10, it prints out 98 numbers, and when I put in 5, it prints out 50 numbers. I've tried this more than once now... any ideas why this is happening please?

you forgot to do

--required

in the for loop (line 14-16)

note: the loop could be made more efficient by adding if(required==0) break ;

Yea you're right, now it works. Thanks so much, you've been amazing. But may I please ask a couple more things: "--required" decrements that variable so that the number entered into the function is the number of numbers printed out right?

And how can I adjust the range pls?I tried putting a variable with a smaller number in the place of 'available' in line 13 & it compiled, but when i ran the program I got an 'Arithmetic Exception'. Sorry to keep hassling like this.

Again, apologies, so embarassing, I solved the 2nd qstn, I have to use the same value in lines 11 & 13 if I'm going to use something different from 'available'.

Thanks v much for all your help vijayan, it's very much appreciated!

see thread at

http://www.daniweb.com/techtalkforums/post345699.html#post345699

this fills an array, instead of printing out the random number; but the idea is the same