random numbers limit

This is a discussion on random numbers limit within the C Programming forums, part of the General Programming Boards category; I wrote this code it generates ramdom numbers but the problem is that it keeps gererating the same random numbers ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    92

    random numbers limit

    I wrote this code it generates ramdom numbers but the problem is that it keeps gererating the same random numbers all the time how do i make it so the code generates different ramdom numbers all the time. Plus I would like to have the random numbers from -0.5 to 2.5
    can some one point me to the right direction.

    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main()
    {
    	
     int i,a;
     
    for (i = 0; i < 10; i++) 
        {
            a=rand();	
            printf("%d\n",a);
        }
       
        return 0;
    }

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Sent from my iPad®

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main()
    {
    	
     int i,a;
     srand(time(0));
    for (i = 0; i < 10; i++) 
        {
            a=rand()%100;   // ranon number with in 100	
            printf("%d\n",a);
        }
       getchar();
        return 0;
    }
    /*myoutput
    87
    79
    3
    63
    68
    70
    2
    15
    86
    94
    */
    ssharish2005

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    To further the point, time.h is not needed at all in your current code. The reason you'll find it included frequently when you see rand used, is because people use time to seed srand. If you aren't seeding with time, then you don't need to include it's header.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    92

    Limit

    Thanks for the code it's now generating time dependant random numbers but I want to limit it betbeen -0.5 and 2.5 what modifications must be made to the code to limit it.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main()
    {
    
        int i,a;
    
        /* Set evil seed (initial seed) */
        srand( (unsigned)time( NULL ) );
    
        for (i = 0; i < 12; i++) 
        {
            a=rand()%100;	
            printf("%d\n",a);
        }
       
        return 0;
    }

  6. #6
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    upper limit can be set like this
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main()
    {
    
        float i,a;
    	float temp;
        /* Set evil seed (initial seed) */
        srand( (unsigned)time( NULL ) );
    
        for (i = 0; i < 12; i++) 
        {
            a=rand()%100;
    		temp=a/39.6f;
            printf("%f\n",temp);
        }
    	
       
        return 0;
    }

  7. #7
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main()
    {
    
        float i,a;
        srand( time( 0 ) );
    
        for (i = 0; i < 12; i++)
        {
    	a=rand()%30;  //now the range is 0 - 29
    	a=a/10-0.5;     //a/10---0 to 2.9xxx subtracting 0.5 from it 
    	printf("%1.1f\n",a);
        }
    
        return 0;
    }
    1 Possible soln would be.
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built
    for"

  8. #8
    Registered User
    Join Date
    Sep 2005
    Posts
    92

    Storing the numbers in an array

    I seen to have over come the problem of generationg time dependant random numbers abd I also midified the code to give me an output in my desired range but I can'nt seem to store it in an array I would find it very helpful if someone cam point me to the right direction and tell me where I went wrong.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define x 10
    
    int main()
    {
    
        float i,a;
        float s[x];
        srand( time( 0 ) );
    
        for (i = 0; i <=10; i++)
        {
    	a=rand()%26;  
    	a=a/11;     
    	s[i]=a;
    	scanf("%1.1f",&s[i]);
    	printf("%1.1f\n",s[i]);
        }
    
        return 0;
    }

  9. #9
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    it should be int i instead of float i

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    If you actually did a board search you'd find a massive amount of random number functions that take a range.

    The basic way is this:
    Take your upper bound and subtract your lower bound. This is now your range.
    Get a number in the set of 0 to range
    Add your lower limit
    You now have a number between your lower limit and your upper limit.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 09:47 AM
  2. Doubts regarding random numbers generation
    By girish1026 in forum C Programming
    Replies: 9
    Last Post: 12-31-2008, 10:47 PM
  3. random numbers
    By h_howee in forum C++ Programming
    Replies: 3
    Last Post: 12-21-2005, 02:56 PM
  4. hi need help with credit limit program
    By vaio256 in forum C++ Programming
    Replies: 4
    Last Post: 04-01-2003, 12:23 AM
  5. Replies: 3
    Last Post: 07-24-2002, 09:46 AM

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