random number between 0 and variable value

This is a discussion on random number between 0 and variable value within the C++ Programming forums, part of the General Programming Boards category; Hey guys, I'm writing an RPG, and i need to generate a random number between 0 and a certain values ...

  1. #1
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355

    random number between 0 and variable value

    Hey guys,

    I'm writing an RPG, and i need to generate a random number between 0 and a certain values content.

    eg.
    Code:
    x = 10;
    
    random = (rand()x); //to generate a number between 0 and 10
    That doesn't seem to work, but it gives you an idea of what i want to do, i've searched, but a lot of stuff comes up when you enter random numbers!

    Thanks.

    -HM

  2. #2
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    Code:
    rand() % x; //???
    Oh and make sure to seed before you do that.

  3. #3
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    thanks for the speedy reply, i knew it would be as simle as that :-/

    btw, i always seed.

  4. #4
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    Originally posted by HybridM
    thanks for the speedy reply, i knew it would be as simle as that :-/

    btw, i always seed.
    Just reminding you since you didn't seem to include that in your demostration.

  5. #5
    Back after 2 years Panopticon's Avatar
    Join Date
    Dec 2002
    Posts
    262
    What is seeding? Why seed?
    I AM WINNER!!!1!111oneoneomne

  6. #6
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    Code:
    srand(time(NULL));
    makes the numbers more random...

  7. #7
    biz
    biz is offline
    Registered User
    Join Date
    Mar 2002
    Posts
    14
    Code:
    #include <time.h>
    #include <stdlib.h>
    
    ...
    
      srand(time(NULL));
      x=(rand()%(N+1)); // Generate random # between 0 and N
      x=(rand()%(end-start+1)+start); // Generate random # between start and end
    
    ...
    Hope this helps...

    ** edit..fixed typo
    Last edited by biz; 02-04-2003 at 05:16 PM.

  8. #8
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280

    Accually...

    Code:
    x=(rand()%N);
    That would generate a random number between 0 and N-1.

  9. #9
    Registered User
    Join Date
    Oct 2002
    Posts
    160
    But I thought % <- was a modulus... how does this have to do with a random number?

    Other then that... what is the difference betwem rand() and srand() along with what does this line do: srand(time(NULL));
    Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
    Hell I might even learn something

  10. #10
    biz
    biz is offline
    Registered User
    Join Date
    Mar 2002
    Posts
    14
    Originally posted by Munkey01
    Code:
    x=(rand()%N);
    That would generate a random number between 0 and N-1.
    oops...yep...you are right...damn typos *edited to fix*

    srand() seeds the random # generator with a value. If you do not srand() before you generate numbers then the same 'random' numbers might show up each time your program runs.
    EX:
    c:\> text.exe
    r1=4
    r2=10
    r3=1

    c:\> text.exe
    r1=4
    r2=10
    r3=1


    I hope this explains it...the numbers are 'random' but only within a single execution (if it is not seeded via srand()) because it uses that same value as the seed value each time. Seeding it with something out of time() is the best way we can seed it with a different seed each time.
    Last edited by biz; 02-04-2003 at 05:20 PM.

  11. #11
    Back after 2 years Panopticon's Avatar
    Join Date
    Dec 2002
    Posts
    262
    Originally posted by biz
    Code:
    #include <time.h>
    #include <stdlib.h>
    
    ...
    
      srand(time(NULL));
      x=(rand()%(N+1)); // Generate random # between 0 and N
      x=(rand()%(end-start+1)+start); // Generate random # between start and end
    
    ...
    Hope this helps...

    ** edit..fixed typo
    Sorry, that didnt help much. What does srand() and time(NULL) do? It would be great if i just knew what those functions do.
    I AM WINNER!!!1!111oneoneomne

  12. #12
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    srand() seeds rand
    time(NULL) seeds ran with time

    sorry that's the best i can do, someone else can explain better

  13. #13
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    rand() produces a series of numbers based on a starting number (it uses the previously generated number to generate the next number). You can change this starting number using srand(). If the starting number is always the same you'll get the same series of numbers each time. If you seed it with a different number (which time() gives you) you'll get a different series each occurance (assuming you're not seeding rand() at exactly the same time).
    Joe

  14. #14
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Read the frigging faq
    FAQ Randomize?????? (C++)

  15. #15
    Registered User
    Join Date
    Oct 2002
    Posts
    160
    the word "seeds" seems strange to me. Plz help me out. (Denmark)

    Oh, and besides that... what is the point of modulus in all this?
    Last edited by Zahl; 02-05-2003 at 12:15 PM.
    Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
    Hell I might even learn something

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

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