2

Below but it always gives me 42 as SIZE. I wanted to randomise SIZE with srand(time(NULL)) but obviously it is not working because it is below randomisation of SIZE. When I try to add it before randomization of SIZE, compiler yells at me. Do you have any ideas how to correct it?

int i,numberToBeFound;
int SIZE=(rand()%100)+1; // size can be in the range [1 to 100]
int *array2 = (int*) malloc(sizeof(int)*SIZE);

srand(time(NULL));

for(i=0;i<SIZE;i++)     //fill the array with random numbers
    array2[i]=rand()%100; 
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Lyrk
  • 1,936
  • 4
  • 26
  • 48

4 Answers4

4
int i, numberToBeFound;
int SIZE=0;
int* array2=0;

srand(time(NULL));

SIZE=(rand()%100)+1; // size can be in the range [1 to 100]
array2 = (int*) malloc(sizeof(int)*SIZE);

for(i=0;i<SIZE;i++)     //fill the array with random numbers
    array2[i]=rand()%100;

It is ancient C (C89 or older). So declare your locals, then init them, and finally use them as needed.

StarPilot
  • 2,246
  • 1
  • 16
  • 18
  • this still does not randomize SIZE, because the seed is done after the rand call. – typ1232 May 02 '13 at 18:39
  • You still call `rand()` before `srand()`. And it's not C, it's C89 [obsolete, invalidated, bad]. – Daniel Fischer May 02 '13 at 18:39
  • 1
    That's not the issue... the issue is that he didn't call `srand()` **before** he called `rand()` – K Scott Piel May 02 '13 at 18:39
  • No, this is the issue, just OP didn't make it clear, see his comment on other answer. – Pete Fordham May 02 '13 at 18:40
  • It is the issue. It is C, not C++. I just forgot to move the srand seeding call up. Code block edited to reflect the correct order. Declare locals, initialize. Use. That is C. – StarPilot May 02 '13 at 18:42
  • The current C standard permits mixed declarations and statements within a block; that's been the case since 1999. The OP is probably using a compiler that only supports C90. @DanielFischer: the code is still valid C99 and C11; refraining from using C99 features doesn't make it "not C". – Keith Thompson May 02 '13 at 18:48
  • The `malloc` call is better written as `array2 = malloc(SIZE * sizeof *array2);` – Keith Thompson May 02 '13 at 18:48
  • @KeithThompson Valid points. But in older C standards, you declare all local variables first. Since the OP stated he moved the call to seed rand before the declares and his compiler screamed at him, that allowed us to deduce his compiler is using and older standard. – StarPilot May 02 '13 at 18:50
  • I am using Visual Studio you are right I will immediately change it Poor Microsoft – Lyrk May 02 '13 at 18:52
  • @KeithThompson Sure the code is valid C. I referred to "It is c. So declare your locals, then init and set them as needed." which I interpret as meaning that C doesn't allow mixed declarations and code. And it is not C that is forbidding mixed declarations and code, but C89. (cf. also [this comment by StarPilot](http://stackoverflow.com/questions/16345288/srand-is-not-working-below-initialization-of-array-randomly/16345354?noredirect=1#comment23414504_16345288) that seems to imply that (s)he believes C forbids it.) – Daniel Fischer May 02 '13 at 19:12
  • @DanielFischer: Ok, it wasn't clear (to me) what "it" referred to in "it's not C". I agree with your point. Unfortunately, not everyone has the luxury of using up-to-date compilers. – Keith Thompson May 02 '13 at 19:15
  • @DanielFischer I'll update my "it's c" statement to make you happy. – StarPilot May 02 '13 at 19:15
  • You didn't have to include the "ancient", saying C89 is enough. Now I can upvote the answer, thanks. – Daniel Fischer May 02 '13 at 19:18
3

You need to call srand() before you call rand() to initialize the random number generator.

You can try srand( time( NULL ) ) which will give you a different result once per second. If you need it to be more variable than that, then you will have to come up with a better way to seed the number generator.

int i,numberToBeFound;
int SIZE;
int *array2;

srand( time( NULL ) );

SIZE=(rand()%100)+1; // size can be in the range [1 to 100]
array2 = malloc(sizeof(int)*SIZE);

for(i=0;i<SIZE;i++)     //fill the array with random numbers
    array2[i]=rand()%100; 

PS: You should not cast malloc() in C -- see this post.

Community
  • 1
  • 1
K Scott Piel
  • 4,320
  • 14
  • 19
  • He does call `srand(time(NULL))`; the problem is that he does it *after* calling `rand()` to initialize `SIZE`. (Your code calls `srand()` twice, which is rarely a good idea.) – Keith Thompson May 02 '13 at 18:36
  • Ah... doh. I didn't look past the `rand()` call once I saw he hadn't called `srand()` yet. ~lol~ Edited accordingly. – K Scott Piel May 02 '13 at 18:38
  • I already know that I have to call srand before rand but because I am using Visual Studio, when I put srand before rand, compiler does not accept it. I asked if there is a workaround of this. – Lyrk May 02 '13 at 18:38
  • seriously? Edited to declare the variables first, then call srand(), then init the variables. I'm stunned you couldn't figure out how to do that. – K Scott Piel May 02 '13 at 18:41
3

You say in a comment that if you call srand() before rand(), your compiler doesn't accept it.

That's because your compiler (Microsoft?) is enforcing C89/C90 rules, which don't permit declarations to follow statements within a block.

You can work around that limitation by adding a new block. A rough outline:

srand(time(NULL));
{
     int size = rand() % 100 + 1;
     /* ... */
}

Or you can remove the initializers for size and array2, replacing them with assignments after calling srand(). Personally, I prefer to initialize variables as early as possible.

Generally speaking, you should call srand() exactly once in your program, before any calls to rand(). (That's for the most general case where you want non-repeating pseudo-random numbers; if you actually need a repeatable sequence, a different strategy is appropriate.)

(Without a call to srand(), rand() behaves as if you had called srand(1), and generates the same sequence each time.)

(I've changed the name of your variable SIZE to size; all-caps identifiers are conventionally used for macros.)

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • that function creation of srand was genius as a workaround. Thanks – Lyrk May 02 '13 at 19:03
  • 1
    @user1939432: That's not "function creation", I just added a block. Syntactically, `{` followed by zero or more declarations and statements followed by `}` is a compound statement, which can appear anywhere a statement can appear *or* as the body of a function. In C90, declarations must precede statements within a block; in C99 and later (and in C++) they can be mixed. – Keith Thompson May 02 '13 at 19:05
1

To not get always the same result from rand you need to seed the generator with srand before doing any calls to rand. So you only need to move your srand call to a place before calling rand.

typ1232
  • 5,535
  • 6
  • 35
  • 51