random doesn't do random

This is a discussion on random doesn't do random within the C Programming forums, part of the General Programming Boards category; Every time I run this program it gives me the same values for the random numbers. 1. 6 2. 6 ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    124

    random doesn't do random

    Every time I run this program it gives me the same values for the random numbers.
    1. 6
    2. 6
    3. 5
    4. 5
    5. 6
    6. 5
    7. 1
    8. 1
    9. 5
    10. 3
    11. 6
    12. 6
    13. 2
    14. 4


    this always stays the same.
    What am I doing wrong?
    The code follows:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    main () {
    
    	int values [14];
    	int i,n;
    	
    	int amount[7]={0};
    	
    	
    	
    	for (i=0; i<14; i++){
    		values[i]=rand()%6+1;
    		n=i+1;
    		printf("%i.  %i\n",n,values[i]);
    		if (values[i]==1){
    			amount[1]++;}
    		else if (values[i]==2){
    			amount[2]++;}
    
    
    	}
    
    	for (i=1; i<7; i++){	
    	
    		printf("%i\n",amount[i]);
    	}
    	system ("Pause");
    
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    that didnt help me any
    ????

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,629
    that didnt help me any
    How did you use srand(), then?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by goran00 View Post
    that didnt help me any
    ????
    Read the bit about seeding.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Main should also return int, and it's better if you use getchar() instead of system("pause").
    But as said, you need to seed the random generator using srand. So read the article again.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random to int?
    By psyadam in forum C# Programming
    Replies: 7
    Last Post: 07-22-2008, 08:09 PM
  2. Lesson #3 - Math
    By oval in forum C# Programming
    Replies: 2
    Last Post: 04-27-2006, 08:16 AM
  3. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 04:51 PM
  4. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM
  5. Best way to generate a random double?
    By The V. in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 04:11 PM

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