Random number range

This is a discussion on Random number range within the C Programming forums, part of the General Programming Boards category; Hi everyone, I need an ideea for generating some ronadom characters to test the code i'm working on. For now ...

  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
    22,258
    I suggest that you read Prelude's article on using rand().
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 06: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, 03:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21