Thread: random doesn't do random

  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

  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
    28,413
    that didnt help me any
    How did you use srand(), then?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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

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, 05: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