1. ## array question

How would you fill an array with random numbers? Then just to complicate it a little, how would you make any of those numbers that are divisable by nine to be negative? Any help in getting me moving in the right direction is appreciated.

Thanks,
DD

2. EDIT: i misread you. i thought you said turn all instances of 9 into -9, thats what this code does
EDIT2: code corrected, makes anything divisible by 9 negative
Code:
```#include <stdio.h>
#include <stdlib.h>

#define NUMBEROFNUMBERS 500

int numbers[NUMBEROFNUMBERS];

#define BIGGESTRANDOMNUMBER 100

int main (int argc, char *argv[])
{ /* fills numbers[] with NUMBEROFNUMBERS pseudorandom ints ranging from 0 to BIGGESTRANDOMNUMBER - 1
* all divisible by 9 are made negative */
int i;
i = atoi (argv[1]); /* user can specify random seed at runtime */
srandom (i);
for (i = 0; i < NUMBEROFNUMBERS; i++)
{
numbers[i] = ((random () >> 20) * BIGGESTRANDOMNUMBER) >> 19;
if (numbers[i] % 9 == 0)
numbers[i] = -numbers[i];
}
return EXIT_SUCCESS; /* i think i spelt it wrong */
}```
might even be ansi-c compatible; dont take my word on it

3. Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define RANGE 200

int function()
{
return rand()%RANGE + 1;
}

int main()
{
int array[10];
int i;
srand((unsigned)time(NULL));

for(i=0;i<10;i++)
array[i] = function();

printf("BEFORE\n");
for(i=0;i<10;i++)
printf("%d ", array[i]);

for(i=0;i<10;i++)
if(array[i] % 9 == 0)
array[i] *= -1;
printf("\nAFTER\n");
for(i=0;i<10;i++)
printf("%d ", array[i]);
return 0;
}```
nuff said.

4. Originally posted by The Dog
Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define RANGE 200

int function()
{
return rand()%RANGE + 1;
}

int main()
{
int array[10];
int i;
srand((unsigned)time(NULL));

for(i=0;i<10;i++)
array[i] = function();

printf("BEFORE\n");
for(i=0;i<10;i++)
printf("%d ", array[i]);

for(i=0;i<10;i++)
if(array[i] % 9 == 0)
array[i] *= -1;
printf("\nAFTER\n");
for(i=0;i<10;i++)
printf("%d ", array[i]);
return 0;
}```
nuff said.
my libc docs dont mention any function srand. what libc are you using out of curiosity?

5. stdlib.h - declarations/definitions for commonly used library functions

I'm using MSVC++

6. figures. i'm using djgpp. stdlib.h:

Code:
`/* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */`
i'm guessing that srand() does the same thing as srandom() tho, judging by the way you used it.

7. what's the difference between rand() and srandom()?

8. srand () or srandom () sets the random seed

rand () or random () returns the random number

9. aaaaaaah, thanks!

10. ## Another Question

Thanks for pointing me in the right direction. I have got it working like I need to now. But I do have another question.

Dog- You used
Code:
`srand((unsigned)time(NULL));`
is this a better seed then using
Code:
`srand(997);`
or just a different way to get the same type of results.

From what I have read so far, using time for the seed is mentioned, but I have only seen the use of a number like 997 being used in all the examples I have to reference.

I would just like to clarify this in my mind so I can understand it better. Thanks again for all the help.

DD

11. ## Re: Another Question

Originally posted by DocDroopy
Thanks for pointing me in the right direction. I have got it working like I need to now. But I do have another question.

Dog- You used
Code:
`srand((unsigned)time(NULL));`
is this a better seed then using
Code:
`srand(997);`
or just a different way to get the same type of results.

From what I have read so far, using time for the seed is mentioned, but I have only seen the use of a number like 997 being used in all the examples I have to reference.

I would just like to clarify this in my mind so I can understand it better. Thanks again for all the help.

DD
if you initialize the psuedorandom seed with the same number, it will return the same pseudorandom numbers. if you initialize the psuedorandom seed from the clock (as dog did), it will return different set of numbers every time.

i initialized the random seed from argv[1], which means i would run the program "program.exe 643809", where 49843643 is a random bunch of numbers i punched out. stupid? yeah. initialize the seed from the clock.

12. >my libc docs dont mention any function srand.
Seeing as how srand is a standard function, you should have some documentation on it.

>i'm guessing that srand() does the same thing as srandom() tho, judging by the way you used it.
Yes, but srand is sure to be there on every implementation, srandom is not.

>numbers[i] = ((random () >> 20) * BIGGESTRANDOMNUMBER) >> 19;
This could very well bite you when you're not looking. The result of right shifting a signed value is implementation defined.

-Prelude

13. Originally posted by Prelude
>my libc docs dont mention any function srand.
Seeing as how srand is a standard function, you should have some documentation on it.

>i'm guessing that srand() does the same thing as srandom() tho, judging by the way you used it.
Yes, but srand is sure to be there on every implementation, srandom is not.

-Prelude
hmm. i checked stdlib.h and it is there, but my libc docs dont mention it. oh well.