Thread: Random number range

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    14

    Random number range

    Hi everyone,

    I need an ideea for generating some ronadom characters to test the code i'm working on.
    For now i want to generate just from A to Z so 65 to 90 in ASCII.
    So my problem is reduced to generating numbers from 65 to 90.
    Can you please give me a hint/ideea of how to restrict rand() between these limits?

    Thank You.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So what have you done so far?

    To a moderator: Could we have the last two posts [and any subsequent ones following this] separated to a new thread, as I don't think it is particularly related to the original subject.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    14

    So far i've worked on the rest of the code where i'm "fighting" with malloc and some nasty pointers, but on the subject i've just searched somehow breiefly on some sites & docs i've bookmarked but nothing suggested any ideea.
    Anyway i didn't asked for someone to solve my task or any code, but only for a hint, so it doesn't seem fair to take me for some guy too lazy to think/search.
    I was just thinking about this while writing the main code. Anyway it's not necessary to use random chars, but because i've used random ints i just thought to do the char part in the same spirit. If the time spent on getting this doesn't pay for me, i'll just use a single char.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It is easy to do, but I think you will learn more by doing it yourself.

    Do you know how to make a random number that is limited to a certain range (e.g. 0..50)?
    If you HAVE a random number that is limited to a certain range, how would you go about shifting it so that the range becomes X...X+50? And finally, what is the range you need to produce alphabetic numbers?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    14
    Quote Originally Posted by matsp View Post
    Do you know how to make a random number that is limited to a certain range (e.g. 0..50)?
    that's exactly my problem; range. i know about RAND_MAX but i don't see how it could help me.
    I thought about making a function that checks if the random number is between 65-90 and then turn it into a char.

    Quote Originally Posted by matsp View Post
    And finally, what is the range you need to produce alphabetic numbers?
    I've already told you; from A to Z I need the range 65 to 90.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, so you are looking to limit the range of a number to 0..X, then you use % (X+1). Does that help?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Jul 2008
    Posts
    14
    ok i got it.

    rand()%X = 0 to X-1;
    so
    MyX=65+rand()%26; // ((90-65)+1);
    you lied, it's not that easy i have a headache now .

    thank you for your patience.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, that's it. The generic form is:
    Code:
    int randrange(int min, max)
    {
       return rand() % (max - min + 1) + min;
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest that you read Prelude's article on using rand().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Given why he is randomizing the data, it may not be necessary for him to make better intervals of values. But yes, its a good article that does present a lot of the issues with randomizing a program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random Number: Mersenne Twister Source Needed
    By mercury529 in forum C++ Programming
    Replies: 12
    Last Post: 11-26-2006, 11:40 AM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. Counting number from a random file
    By kamisama in forum C Programming
    Replies: 42
    Last Post: 02-22-2005, 05:16 PM
  4. Random Numbers within a range OTHER than 1-X
    By Kaelin in forum C++ Programming
    Replies: 11
    Last Post: 02-16-2005, 11:57 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