how do i make a random value?

Discussion in 'Programming General' started by ilovegold69, Apr 30, 2012.

how do i make a random value?
  1. Unread #1 - Apr 30, 2012 at 8:20 PM
  2. ilovegold69
    Joined:
    Aug 28, 2011
    Posts:
    1,195
    Referrals:
    0
    Sythe Gold:
    77
    Doge I'm LAAAAAAAME

    ilovegold69 Guru

    how do i make a random value?

    Basically i'm just startin 2 learn c++ and I'm screwin around but i cant seem to figure out how to say... make int value=(integer between 0 and 100) i googled it but all i figured out was something like this
    Value=(rand()%100)+1;
    I mean it spits out a number but its like 42 every time so how do i get a completely random integer between 0 and 100 every time?
    Copy and paste the code here and i'll figure it out... or u can explain whatever

    Thanx
     
  3. Unread #2 - May 1, 2012 at 3:30 AM
  4. Darthatron
    Joined:
    May 22, 2006
    Posts:
    1,612
    Referrals:
    3
    Sythe Gold:
    0

    Darthatron Massive Troll
    Retired Sectional Moderator Visual Basic Programmers

    how do i make a random value?

    You need to seed the random number generator. :)

    If you seed it to the current time it will be more random.

    Code:
    srand(time(0));
    int value1 = (rand() % 100) + 1;
    int value2 = (rand() % 100) + 1;
    That will return two random numbers.

    http://www.cplusplus.com/reference/clibrary/cstdlib/rand/
     
  5. Unread #3 - May 2, 2012 at 11:04 PM
  6. ilovegold69
    Joined:
    Aug 28, 2011
    Posts:
    1,195
    Referrals:
    0
    Sythe Gold:
    77
    Doge I'm LAAAAAAAME

    ilovegold69 Guru

    how do i make a random value?

    thanks! why would anyone wanna use just Value=(rand()%100)+1; ?
     
  7. Unread #4 - May 3, 2012 at 1:01 AM
  8. The Fat Controller
    Joined:
    Aug 16, 2007
    Posts:
    1,003
    Referrals:
    0
    Sythe Gold:
    1

    The Fat Controller Guru

    how do i make a random value?

    No one would want to - the number generator needs to be seeded for it to actually generate "random" numbers.

    Using the same seed causes the number generator to always produce the same sequence of numbers. Using different ones (time(0) is usually different each time it's called) causes different sequences of numbers to be produced each time.
     
< [dumps<=>ccv <=>track1&2 <=> Italy<=>usa<=>uk<=>bank Logins<=>transfer To Any Bank<=> | 40+ C++ E-books[Organized][Google doc's][No download required] >

Users viewing this thread
1 guest


 
 
Adblock breaks this site