Thread: Random Interger

  1. #1
    Registered User Seph_31's Avatar
    Join Date
    Nov 2003

    Random Interger

    How do i make a random interger and set the min, max?
    #include <stdio.h>
    /* Random int here */
    int randomint = 0;
    int main()
      return 0;

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    FAQ > How do I... (Level 1) > Generate random numbers?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    well the min and max is 0 to RAND_MAX, I think that is defined in stdlib.h, I guess you could undef it to change max but most of us just use modulo operator to correct the result.
    look at rand() and srand().
    // Dave_Sinkula, you beat me
    Help populate a c/c++ help irc channel
    server: irc://
    channel: #c

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > I guess you could undef it to change max
    You can't undef RAND_MAX to change it's value
    Use the modulo operator
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

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