array question

This is a discussion on array question within the C Programming forums, part of the General Programming Boards category; How would you fill an array with random numbers? Then just to complicate it a little, how would you make ...

  1. #1
    Registered User DocDroopy's Avatar
    Join Date
    Jul 2002
    Posts
    32

    Question 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
    "aut vincere aut mori"

  2. #2
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    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
    Last edited by moi; 07-27-2002 at 08:40 PM.

  3. #3
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    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. #4
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    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. #5
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    stdlib.h - declarations/definitions for commonly used library functions
    Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.

    I'm using MSVC++

  6. #6
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    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. #7
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    what's the difference between rand() and srandom()?

  8. #8
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    srand () or srandom () sets the random seed

    rand () or random () returns the random number

  9. #9
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    aaaaaaah, thanks!

  10. #10
    Registered User DocDroopy's Avatar
    Join Date
    Jul 2002
    Posts
    32

    Question 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
    "aut vincere aut mori"

  11. #11
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946

    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. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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.

  13. #13
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 06:07 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 05:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21