A Real Urgent Srand Problem!!! Pleeese Help.

This is a discussion on A Real Urgent Srand Problem!!! Pleeese Help. within the C Programming forums, part of the General Programming Boards category; I am tyrying to get SRAND to generate a random number between 50 and 100. My code works but generates ...

  1. #1
    Registered User Robert_Ingleby's Avatar
    Join Date
    Oct 2001
    Posts
    57

    A Real Urgent Srand Problem!!! Pleeese Help.

    I am tyrying to get SRAND to generate a random number between 50 and 100.
    My code works but generates 1-100, what do I have to do to the %100 to get it to go between 50 and 100.

    Here is the code:

    include <stdlib.h>
    #include <stdio.h>
    #include <time.h>

    int main(void)
    {
    int i;
    time_t t;

    srand((unsigned) time(&t));

    for(i=0; i<10; i++)
    printf("%d\n", rand() % 100);
    return 0;
    }

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    If you did modulus 50

    x % 50,

    Your results would be between 0 and 49

    Therefore, by adding 50 to the variable result, you will get a range between 50 & 99.........

    Hows that?

  3. #3
    Registered User Robert_Ingleby's Avatar
    Join Date
    Oct 2001
    Posts
    57

    IS THERE ANOTHER WAY

    Is this the only way? Im sure there is another way of generating random numbers in a range.

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    >>Is this the only way? Im sure there is another way of generating random numbers in a range.

    Uhh?.....whats wrong with that?

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Look;

    Code:
    #include <stdlib.h>
    #include <stdio.h> 
    #include <time.h> 
    
    int main(void) 
    { 
    int i; 
    time_t t; 
    
    srand((unsigned) time(&t)); 
    
    for(i=0; i<10; i++) 
    printf("%d\n", ((rand() % 50) + 50));
    return 0; 
    }
    Isnt that straight forward enough?

  6. #6
    Registered User Robert_Ingleby's Avatar
    Join Date
    Oct 2001
    Posts
    57

    Heres why.

    Its not how I did it last year.

    Firstly my programs in a continuous loop and seccondly the number range needs changing frequently thirdly there are 5 srands running at once.
    The ammount of variables needed for this would be astronomical!

    It was a real neat piece of code i wrote but I could kick myself for not remembering it.

    I appreciate your help, but if you have no more ideas then kindly refrain from posting.

    Thanks.

  7. #7
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Fine.....you didnt mention any more info or requirements in the original question or your subsiquent posts so how was I to guess your specifications?.....

    Never mind.....good luck

  8. #8
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    The ammount of variables needed for this would be astronomical!
    Why? If you want to change the ranges frequently then regardless of what method you use, you're going to have to store these ranges somewhere. You are using two variables for each rand(), a variable for 'to' and one for 'from'. How could you reduce this?
    zen

  9. #9
    Registered User Robert_Ingleby's Avatar
    Join Date
    Oct 2001
    Posts
    57

    ok Genius, answer this!!

    If I want random numbers bertween 9 and 15, using modulus %9 + 6 then some numbers will be less than 9!.

  10. #10
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    If you want random numbers between 9 and 15 then why would you do %9 + 6?

    If I were you I'd go back and read the FAQ, like I told you yesterday (only this time read it).
    zen

  11. #11
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    try %7+9 instead....

    lol@zen..... is that steam coming from your ears mate?
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  12. #12
    Registered User Robert_Ingleby's Avatar
    Join Date
    Oct 2001
    Posts
    57

    Zen and Stoned Coder

    Zen,

    I have READ the FAQ's, but it dont say how to use SRAND for a number in a range.

    Stoned,

    No, its high pressure steam mate!!!!!!!

  13. #13
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Taken, directly from the link I posted -

    Q: But, how do i get a number between x and y? Like a die or something?
    A: Use a function like this one...

    int rand_mid(int low, int high)
    {
    return low+rand()%(high-low+1);
    }
    zen

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > Zen,
    >
    > I have READ the FAQ's, but it dont say how to use SRAND for a > number in a range.

    You may have read it, but you damn sure don't understand it.

    You do not have "multiple srands". You call "srand" once. Period.

    srand stands for "seed random". If you know anything about random number generation, you know you base all random numbers off of a 'seed'.

    Once ou have 'seeded' the random number generator, then you can finally start producing random numbers. To do this in C, using the standard library, you use "rand()".

    To produce numbers in a range, you call rand() % MAX_NUMBER, which will produce numbers in the range of 0 to MAX_NUMBER-1. Thus, if you want the number range "5 - 15", you do:

    number = rand() % 11 + 5;

    Which will produce the number range 0-10(+5).

    Now, what part of that was hard to understand, moron. Don't just show up and be a wise ass and disregard peoples honest attempt at help when you obviously have no idea what you're talking about.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Factorial
    By foxman in forum Contests Board
    Replies: 27
    Last Post: 07-11-2008, 07:59 PM
  2. %16 with double
    By spank in forum C Programming
    Replies: 11
    Last Post: 03-05-2006, 10:10 PM
  3. Urgent Programming Problem
    By mchap1643 in forum C Programming
    Replies: 5
    Last Post: 10-16-2003, 09:54 AM
  4. Programming Puns
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 44
    Last Post: 03-23-2002, 04:38 PM
  5. Replies: 7
    Last Post: 12-12-2001, 10:28 AM

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