Thread: randoms...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    731

    randoms...

    I know how to use randoms for the most part...but I never found out if you could make it pick a number from (like) 5-10. Instead of 0-10.

    Please help.

  2. #2
    the Wizard
    Join Date
    Aug 2004
    Posts
    109
    You can, here's the example that I use the most:

    Code:
    int rNum(int lowest_number, int highest_number) {
     int range = highest_number - lowest_number + 1;
     return lowest_number + int(range * rand()/(RAND_MAX + 1.0));
    }
    -//Marc Poulsen -//MipZhaP

    He sat down, he programmed, he got an error...

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    Alright, thanks.

  4. #4
    the Wizard
    Join Date
    Aug 2004
    Posts
    109
    Np there...
    -//Marc Poulsen -//MipZhaP

    He sat down, he programmed, he got an error...

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by MipZhaP
    You can, here's the example that I use the most:

    Code:
    int rNum(int lowest_number, int highest_number) {
     int range = highest_number - lowest_number + 1;
     return lowest_number + int(range * rand()/(RAND_MAX + 1.0));
    }
    This doesn't work with gcc on my system (Windows XP). For example, if you use lowest_number = 1, and highest_number = 10, the result is always 1. For anyone who wants to try it, have a go at this:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int rNum(int lowest_number, int highest_number) {
     int range = highest_number - lowest_number + 1;
     return lowest_number + (int)(range * rand()/(RAND_MAX + 1.0));
    }
    
    int rNum2(int lowest_number, int highest_number) {
     int range = highest_number - lowest_number + 1;
     return lowest_number + (int)(range * (rand()/(RAND_MAX + 1.0)));
    }
    int main()
    {
      int i;
    
      for (i = 0; i < 20; i++) {
        printf("rNum() = %2d,  rNum2() = %2d\n", rNum(1, 10), rNum2(1,10));
      }
    
      return 0;
    }

    Hints for people who don't have different compilers: for Borland C++ and Microsoft Visual C++, RAND_MAX = 32767. For gcc, RAND_MAX = 2147483647

    (gcc version 3.3.1, Borland C++ version 5.5.1, Visual Studio 6.0)

    Dave

  6. #6
    Banned
    Join Date
    Aug 2004
    Posts
    5
    The problem is easily remedied by casting rand() to a float or double.
    Code:
    return lowest_number + (int)( ((double)rand())/RAND_MAX );

  7. #7
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    so whats the differeence between gcc and c++?

  8. #8
    Banned
    Join Date
    Aug 2004
    Posts
    5
    gcc compiles the code as C
    g++ compiles the code as C++

  9. #9
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    ok so wich one is better?

  10. #10
    Banned
    Join Date
    Aug 2004
    Posts
    5
    Well if you are compling a C program use gcc, if you are compiling a c++ program use g++

  11. #11
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Rune Hunter
    so whats the differeence between gcc and c++?
    gcc compiles C programs
    g++ compiles C++ programs

    The program that I submitted is a valid C program and a valid C++ program, so use either.

    "Real" C++ programmers would make fun of C++ programs that look like C, and they would surely put in some stuff that would prevent compilation as a C program. If you want to use <iostream> and use cout << instead of printf, it will be a C++ only program, and it would be more appropriate on this C++ board, but the results are the same.

    Regards,

    Dave
    Last edited by Dave Evans; 08-26-2004 at 03:40 PM.

  12. #12
    Teenage Mutant Ninja Nerd MMD_Lynx's Avatar
    Join Date
    Aug 2004
    Posts
    65
    my way to do randoms is different (it doesn't involve RAND_MAX)

    something like
    Code:
    int randnum(int min, int max)
    {
       return min + (rand() % (max - min +1))
    }
    i thinking that's right
    Stupid people are useful. You can make them do all the mindless tasks you are too lazy to do yourself.

    Sphynx cats are just bald and wrinkly, like old people, and we don't reject them.

  13. #13
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by MMD_Lynx
    my way to do randoms is different (it doesn't involve RAND_MAX)

    something like
    Code:
    int randnum(int min, int max)
    {
       return min + (rand() % (max - min +1))
    }
    i thinking that's right
    I have spoken to statistics "experts", and they are, in general, opposed to using the modulo operator to scale the range of numbers from rand(). Now, different implementations of rand() may be "better" than others, but they are often based on integer multiplication and overflow. That is, they are calculated modulo RAND_MAX. Now, it is possible that the values obtained from rand() are "OK", that is are more-or-less uniformily distributed from 1 to RAND_MAX, but the lower few bits may not be uniformly distributed. Therefore, using RAND_MAX is preferred to using modulo 10, for example.

    Just my .02 euros. Your Mileage May Vary.

    Regards,

    Dave

  14. #14
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Also another problem with modulo operator is that you couldn't choose a range that exceeds RAND_MAX but with division by RAND_MAX you could choose any range you want.

  15. #15
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    whats wrong with plain and simple

    for a random number >=5 & <= 10.

    int num = (rand % 6) + 5;
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Randoms
    By Wick in forum C++ Programming
    Replies: 6
    Last Post: 08-18-2003, 07:21 PM
  2. increasing prob with randoms
    By scuba22 in forum C++ Programming
    Replies: 3
    Last Post: 10-23-2002, 07:06 AM