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
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
"aut vincere aut mori"
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
might even be ansi-c compatible; dont take my word on itCode:#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 */ }
Last edited by moi; 07-27-2002 at 08:40 PM.
nuff said.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; }
my libc docs dont mention any function srand. what libc are you using out of curiosity?Originally posted by The Dog
nuff said.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; }
stdlib.h - declarations/definitions for commonly used library functions
Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
I'm using MSVC++
figures. i'm using djgpp. stdlib.h:
i'm guessing that srand() does the same thing as srandom() tho, judging by the way you used it.Code:/* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
srand () or srandom () sets the random seed
rand () or random () returns the random number
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
is this a better seed then usingCode:srand((unsigned)time(NULL));
or just a different way to get the same type of results.Code:srand(997);
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
"aut vincere aut mori"
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.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
is this a better seed then usingCode:srand((unsigned)time(NULL));
or just a different way to get the same type of results.Code:srand(997);
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
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.
>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
Last edited by Prelude; 07-28-2002 at 06:25 PM.
My best code is written with the delete key.
hmm. i checked stdlib.h and it is there, but my libc docs dont mention it. oh well.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