Adblock breaks this site

how do i make a random value?

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

  1. ilovegold69

    ilovegold69 Guru

    Joined:
    Aug 28, 2011
    Posts:
    1,195
    Referrals:
    0
    Sythe Gold:
    77
    Doge I'm LAAAAAAAME
    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
     
  2. Darthatron

    Darthatron Massive Troll
    Retired Sectional Moderator Visual Basic Programmers

    Joined:
    May 22, 2006
    Posts:
    1,612
    Referrals:
    3
    Sythe Gold:
    0
    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/
     
  3. ilovegold69

    ilovegold69 Guru

    Joined:
    Aug 28, 2011
    Posts:
    1,195
    Referrals:
    0
    Sythe Gold:
    77
    Doge I'm LAAAAAAAME
    how do i make a random value?

    thanks! why would anyone wanna use just Value=(rand()%100)+1; ?
     
  4. The Fat Controller

    The Fat Controller Guru

    Joined:
    Aug 16, 2007
    Posts:
    1,003
    Referrals:
    0
    Sythe Gold:
    1
    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] >


 
 
Adblock breaks this site