Thread: Random Number Range Problem.

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    10

    Random Number Range Problem.

    Hello all,
    I am trying to create a random number between a range of certain numbers, lets say 1 -10. I am using this below but for some reason it gives me the same number most of the time.

    Code:
    int rand_func(int min, int max ) 
    //this function takes 2 arguments: min and max 
    // and creates random numbers based on min an max and loads it into i
    
    {
    
    int i = 0;
    i = rand(); 
    
    printf("\t%d", i);
    
    i = min + (max - min) * i / RAND_MAX; // min and max forumula
    printf("\t%d\n",i);
    
    
    
    
    
    
      return i;
    
    }
    I appreciate any response. Thank You!
    Last edited by xamlit; 01-21-2006 at 12:33 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I know this one, I KNOW THIS ONE - Wooooooo!!!!!
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    Finally, I get to answer a post where the answer is in the FAQ - rejoice.

  3. #3
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    :wq

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    95

    You probaly got your answer

    Quote Originally Posted by xamlit
    Hello all,
    I am trying to create a random number between a range of certain numbers, lets say 1 -10. I am using this below but for some reason it gives me the same number most of the time.
    It does'nt give you the same number most of the time.
    But max isn't behaving.

    Code:
    int rand_func(int min, int max ) 
    {
    int i = 0;
    i = rand(); 
    
    /* printf("%d : ", i); */
    i = min + (max - min) * i / RAND_MAX; 
    /* printf("\t%d\n\n",i); */
    
    return i;
    }
    
    
    void calc_avg(int min, int max, int cnt)
    {
    int  range;
    int  i;
    int  res;
    int  arr[100];
    
    
    range = max - min + 1;
    if ( range > sizeof(arr)/sizeof(arr[0]) )
       {
       fprintf(stderr,"Enlarge arr to %d\n", range);
       return;
       }
    
    memset(arr, 0, sizeof(arr));
    
    for ( i = 0 ; i < cnt ; ++i )
        {
        res = rand_func(min, max);
        ++arr[ res - min ] ;
        }
    
    printf("Resultes with min = %d max = %d. numbers of repeats %d :\n"
             ,min, max, cnt);
    printf("Best stat is %.4f%%\n", 1.0/range*100.0);
    for ( i = 0 ; i < range ; ++i)
        printf("%4d : %7d  stat = %.4f%%\n",
                i + min, arr[i], 
                (arr[i]/(double)cnt)*100.0);
    }
    
    
    
    int  main(void)
    {
    
    calc_avg(6, 8, 9999999);
    calc_avg(1, 10, 9999999);
    calc_avg(12001, 12009, 9999999);
    calc_avg(19001, 19015, 9999999);
    getchar();
    
    return(0);
    }

    Resultes with min = 6 max = 8. numbers of repeats 9999999 :
    Best stat is 33.3333%
    6 : 5001173 stat = 50.0117%
    7 : 4998537 stat = 49.9854%
    8 : 289 stat = 0.0029%
    Resultes with min = 1 max = 10. numbers of repeats 9999999 :
    Best stat is 10.0000%
    1 : 1112069 stat = 11.1207%
    2 : 1110040 stat = 11.1004%
    3 : 1110804 stat = 11.1080%
    4 : 1109341 stat = 11.0934%
    5 : 1110923 stat = 11.1092%
    6 : 1111714 stat = 11.1171%
    7 : 1110934 stat = 11.1093%
    8 : 1112303 stat = 11.1230%
    9 : 1111575 stat = 11.1158%
    10 : 296 stat = 0.0030%
    Resultes with min = 12001 max = 12009. numbers of repeats 9999999 :
    Best stat is 11.1111%
    12001 : 1249934 stat = 12.4993%
    12002 : 1248477 stat = 12.4848%
    12003 : 1251114 stat = 12.5111%
    12004 : 1247631 stat = 12.4763%
    12005 : 1250520 stat = 12.5052%
    12006 : 1249625 stat = 12.4963%
    12007 : 1251098 stat = 12.5110%
    12008 : 1251295 stat = 12.5130%
    12009 : 305 stat = 0.0031%
    Resultes with min = 19001 max = 19015. numbers of repeats 9999999 :
    Best stat is 6.6667%
    19001 : 712749 stat = 7.1275%
    19002 : 712608 stat = 7.1261%
    19003 : 714874 stat = 7.1487%
    19004 : 714646 stat = 7.1465%
    19005 : 715011 stat = 7.1501%
    19006 : 714185 stat = 7.1419%
    19007 : 715245 stat = 7.1525%
    19008 : 714332 stat = 7.1433%
    19009 : 714068 stat = 7.1407%
    19010 : 713911 stat = 7.1391%
    19011 : 714799 stat = 7.1480%
    19012 : 714357 stat = 7.1436%
    19013 : 714453 stat = 7.1445%
    19014 : 714458 stat = 7.1446%
    19015 : 303 stat = 0.0030%

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    @dude543: How did you get this results ?
    The rand_func(int min, int max ) as posted can only return 2 values ( min most of the time, max sometimes ).

    Kurt

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    95

    Not sure

    I am not sure if I got you right. but rand_func should return one value in the range min, max.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I do understand what you want it to return but:
    Code:
    i = min + (max - min) * i / RAND_MAX;
    here you divide the int i by anoter int RAND_MAX that means the result is another int that is always 0 if i is smaller then RAND_MAX.
    So this function should return min most of the time.
    Kurt

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    95

    Try to run the numbers on paper

    Code:
    Lets say for exapmle :
    min = 6 and max = 8
    
    Lets assume RAND_MAX=13
    so we get
    
    i = 6 + 2 * {0,13}/13;
    
    for rand>= 0 && rand<=6   we get i = 6
    for rand>= 7 && rand<=12   we get i = 7
    for rand>= 13 && rand<=13   we get i = 8

  9. #9
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    {0,13}/13; is an integer division -> result is 0 for any value but 13.
    it works only if you cast. e.g

    Code:
    i = min + (max - min) * (double)i / RAND_MAX;
    try this if you don't believe

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int rand_func(int min, int max )  {
       int i = 0;
       i = rand();
    
       i = min + (max - min) * i / RAND_MAX;
    
       return i;
    }
    
    int rand_func_1(int min, int max )  {
       int i = 0;
       i = rand();
    
       i = min + (max - min) * (double)i / RAND_MAX;
    
       return i;
    }
    
    int main() {
       int i;
       for ( i =0; i < 10; ++i )
          printf( "%d ", rand_func( 5, 10 ) );
       puts("");
    
       for ( i =0; i < 10; ++i )
          printf( "%d ", rand_func_1( 5, 10 ) );
    }
    my output
    Code:
    5 5 5 5 5 5 5 5 5 5
    9 5 7 9 6 9 5 9 5 9
    Kurt

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    95

    I added parthince

    They are in red.

    Code:
    Lets say for exapmle :
    min = 6 and max = 8
    
    Lets assume RAND_MAX=13
    so we get
    
    // i = 6 + 2 * {0,13}/13;
    
    i = 6 + ( 2 * {0,13} )  /13;
    
    for rand>= 0 && rand<=6   we get i = 6
    for rand>= 7 && rand<=12   we get i = 7
    for rand>= 13 && rand<=13   we get i = 8

  11. #11
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    No comment.

  12. #12
    old man
    Join Date
    Dec 2005
    Posts
    90
    I think the best idea is to read the faq, which will inspire something like this:

    Code:
    int
    get_rand (int min, int max)
    {
      static int seeded = 0;
      if (seeded == 0)
      {
        srand (time (NULL));
        seeded = 1;
      }
      return rand() % (max - min + 1) + min;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random number gen & looping probelm :?
    By FoxeySoulSLayer in forum C++ Programming
    Replies: 1
    Last Post: 04-10-2009, 05:44 PM
  2. Help regarding random number
    By Bargi in forum C Programming
    Replies: 6
    Last Post: 03-11-2009, 01:16 PM
  3. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  4. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM