help with random number range

This is a discussion on help with random number range within the C Programming forums, part of the General Programming Boards category; here is my program Code: #include<stdio.h> #include<time.h> main() { float i,r; srand( time( 0 ) ); for(i=1;i<5;i++){ r=rand()%40; r=r/5; if(i>1){ ...

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

    help with random number range

    here is my program
    Code:
    #include<stdio.h>
    #include<time.h>
    
    main() {
           
           float i,r;
           srand( time( 0 ) );
           
           for(i=1;i<5;i++){
                              r=rand()%40;
                              r=r/5;
                              
                              if(i>1){
                              printf("%1.0f\n",r);
                              }
                              }
                              
           printf("Hit any key to exit\n");
           getchar();
                              
           return 0;
           }
    I don't know how to get it to exclude 0, i've tried if statements already and such. Also I don't know how to set up the minum value of the random numbers, can anyone help me with that. Like say I wanted to do 2-8...Thanks in advance.

  2. #2
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    just do
    Code:
    r=rand()%40 +1;

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    17
    thanks,now to get the range higher I would just add more then 1 correct?

  4. #4
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Why don't you try a simple set of test cases, and view your result?

    Better yet, think about what the expression is doing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random Number: Mersenne Twister Source Needed
    By mercury529 in forum C++ Programming
    Replies: 12
    Last Post: 11-26-2006, 11:40 AM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 06:20 AM
  3. Counting number from a random file
    By kamisama in forum C Programming
    Replies: 42
    Last Post: 02-22-2005, 05:16 PM
  4. Random Numbers within a range OTHER than 1-X
    By Kaelin in forum C++ Programming
    Replies: 11
    Last Post: 02-16-2005, 11:57 AM
  5. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 03:40 PM

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