Thread: Creating a random number

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    145

    Creating a random number

    I need to create a random integer between 0 and 255, what function would I use and how would I accomplish this?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    rand()%256
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    Also, how can I cast this number to a char?

  4. #4
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Code:
    char thechar[3];
    int num = rand()%256;
    itoa(num, thechar, 10);            //Note that this is not ANSI standard C.
    EDIT:Not a cast, but a function.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    Ah ok thanks.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    My compiler didn't like itoa at all, are there any alternatives I can use?

  7. #7
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    I particularly don't see the problem with casting your number to a char, so long as it's between 0 and 255.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    Yes but how do I do it?

  9. #9
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Code:
    int d = 65;
    char c = (char)d;
    Voila.

    In fact, I remember somewhere reading that you should keep chars in ints anyways...but I don't know if that's accurate at all.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    Ah cheers.

  11. #11
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Code:
    char thechar[3];
    int num = rand()%256;
    sprintf(thechar, "%d", num);
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  12. #12
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    I'm having a problem with the the random function. It's not very random.

    Everytime I run the program, it is picking the same numbers.

  13. #13
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Seed rand:
    Code:
    srand(time(NULL));
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  14. #14
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    You should call it once - for example in the beginning of main
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  15. #15
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    I get this message:

    warning: implicit declaration of function ‘time’

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random Number Issues
    By sansterre in forum C++ Programming
    Replies: 8
    Last Post: 05-15-2009, 05:59 PM
  2. random number gen & looping probelm :?
    By FoxeySoulSLayer in forum C++ Programming
    Replies: 1
    Last Post: 04-10-2009, 05:44 PM
  3. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  4. random number problems
    By lespaul5895 in forum C Programming
    Replies: 3
    Last Post: 07-05-2007, 02:16 AM
  5. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM