Creating a random number

This is a discussion on Creating a random number within the C Programming forums, part of the General Programming Boards category; I need to create a random integer between 0 and 255, what function would I use and how would I ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    rand()%256
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    You should call it once - for example in the beginning of main
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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

    warning: implicit declaration of function ‘time’

Page 1 of 2 12 LastLast
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, 06:59 PM
  2. random number gen & looping probelm :?
    By FoxeySoulSLayer in forum C++ Programming
    Replies: 1
    Last Post: 04-10-2009, 06:44 PM
  3. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 02:31 AM
  4. random number problems
    By lespaul5895 in forum C Programming
    Replies: 3
    Last Post: 07-05-2007, 03:16 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