Like Tree3Likes
  • 1 Post By c_dever
  • 1 Post By phantomotap
  • 1 Post By phantomotap

random numbers adding up..

This is a discussion on random numbers adding up.. within the C++ Programming forums, part of the General Programming Boards category; hello! I've made my first random number generator, and everytime i try it out, the numbers are random, but they ...

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    15

    random numbers adding up..

    hello!
    I've made my first random number generator, and everytime i try it out, the numbers are random, but they all add up. so if i have a randomness range between 1 and 52 (card game) it's randomness is like this:

    3, 12, 18, 25, 30, 32, 41, 49

    and that is not really random i think :P

    this is the source code

    Code:
    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    
    
    using namespace std;
    
    
    int randomRange (int lowest, int highest) {
    return rand() % (highest - lowest) +lowest;  //random number between the lowest and highest,
    }                                           //including the highest (see, +lowest)
    
    
    
    
    int main () {
        srand(time(NULL));
        cout << randomRange(1,52);
    }
    anyone knows whats up?

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    If you can reproduce this same behaviour multiple times, your compiler's implementation of rand() is not very good.
    C++11 allows you to choose the exact algorithm for generating random numbers, via the <random> header file.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    15
    When i do a for loop, going trough this random generating code 1000 times it is generated randomly, but everytime i stárt the program, it's usually a little more than the last time i ran the program

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    4
    Not sure what the issue is. The code basically generates a random number between 1 and 52. If you want the random generator to display the random number n only once (so that the numbers 1-52 do not get repeated), you must add more logic to perform that type of behavior. How about some ouput of your console and additional details on what you want the code to do.
    Last edited by c_dever; 05-05-2012 at 05:58 PM.
    stahta01 likes this.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    2,577
    You might check time(NULL) just to make sure the function is working correctly.
    It is not likely to be wrong; but, it is worth checking because it is easier to check.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,261
    O_o

    Let me guess, you are executing the program with little to no time between runs.

    Soma
    stahta01 likes this.

  7. #7
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by phantomotap View Post
    O_o

    Let me guess, you are executing the program with little to no time between runs.

    Soma
    Wouldn't that yield the same or similar series' ?..and not ones in which every number is greater than its predecessor ?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  8. #8
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,261
    Wouldn't that yield the same or similar series' ?..and not ones in which every number is greater than its predecessor ?
    You are, for some reason, seeing two things that exhibit similar behavior and screaming "These are caused by different things!".

    The default assumption should be that things having similar symptoms are caused by the same underlying condition until further evidence is obtained.

    If he were printing out longer runs of random samples the series itself would be similar or slightly showing a mutation related to the underlying implementation.

    He is only printing out a single random sample.

    The only value he prints is obviously going to fall victim to the same underlying condition that causes a series to exhibit repeated sameness or slight mutation.

    You may look at it like this to confirm the problem for yourself because you seem to understand the seed related to time: he is printing a series that has exactly one element.

    [Edit]
    None of this, by the way, suggests anything about the quality of the underlying implementation.

    If a "PRNG" didn't produce the same sequence with the same seed it would be all but useless as a "PRNG".

    Using the current time as a seed without allowing time for the seed to change is the problem. It naturally depends on the implementation of the "PRNG" and how time is handled on the host system but the problem is generally related to how many samples must be processed before the least significant changes to a seed propagates from the "avalanche" effect.

    As a matter of interest, if you sample one value of random data from a known seed (say 12345) and thoroughly mix the seed obtained from the time you can generally make the least significant bits, the parts of time that are most likely to change, more significant causing faster "avalanche" effects.
    [/Edit]

    Soma

    Name:  silly.png
Views: 131
Size:  9.7 KB
    Last edited by phantomotap; 05-05-2012 at 07:20 PM.
    manasij7479 likes this.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    First of all, that does not generate numbers in the correct range.
    The range calculation requires a +1:
    Code:
    rand() % (highest - lowest + 1) + lowest
    I would also add an assert that highest >= lowest since you wouldn't want to divide by zero, or worse, would you?

    The next problem is with how you are running the program. I'm guessing that you're scripting it in something like a batch file and due to that the seed upon each run is almost the same. Don't worry about this. If you write a real program, i.e. something that doesn't exit within a millisecond, then it will not be a problem.
    (A better PRNG wont help much if the problem is the way it is being seeded)

    Make sure that in your real program, you put the srand call near the top of main and only ever call it once.

    Lastly, if you really want accurate unbiased results, then you need to learn about the rejection method as per post 6 here. And maybe, just maybe, you'll also need a better PRNG.
    Last edited by iMalc; 05-05-2012 at 07:26 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by phantomotap View Post
    he is printing a series that has exactly one element.
    Now I see...
    I thought the code in the OP was just a demo and each run of the real program (card game, as mentioned ) produced a sequence of numbers showing the same trend.
    That was a quite stupid assumption.. :S
    Last edited by manasij7479; 05-05-2012 at 07:34 PM.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  11. #11
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,261
    That was a quite stupid assumption..
    ^_^;

    You assumed that a newbie was using `srand', `time', and `rand' correctly?

    Wow. That is a stupid assumption. ^_~

    Soma

  12. #12
    Registered User
    Join Date
    May 2012
    Posts
    15
    I am a noob, i admit, but i am trying hard
    I just love coding so much :P
    But seeing this, i thought of something. i understand that you can get the seed by the srand(time(NULL)); , but by putting those #include <cstdlib> and #include <ctime> i don't need to make the function rand() right? or do you need to write it somewhere at the start of a program as a prototype or something?

  13. #13
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by Reinmaster7 View Post
    I am a noob, i admit, but i am trying hard
    I just love coding so much :P
    But seeing this, i thought of something. i understand that you can get the seed by the srand(time(NULL)); , but by putting those #include <cstdlib> and #include <ctime> i don't need to make the function rand() right? or do you need to write it somewhere at the start of a program as a prototype or something?
    rand() is defined in <cstdlib> and time() is defined in <ctime>, if you include those headers you can use them right away by just calling them.

    Also, i would advice against ever writing your own implementation of a PRNG and then calling it "rand()".
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  14. #14
    Registered User
    Join Date
    May 2012
    Posts
    15
    Quote Originally Posted by Neo1 View Post
    i would advice against ever writing your own implementation of a PRNG and then calling it "rand()".
    I don't understand this.
    i dont entirelyunderstand the rand statement, but i do think to know that this is a function that takes the socalled seed from the srand function, and makes calculation with it to generate random numbers. i dont write my own impementation of a PRNG, i dont even know what that is xD

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding numbers to a random number
    By Euan_R in forum C++ Programming
    Replies: 5
    Last Post: 01-24-2012, 11:11 AM
  2. Adding Random Numbers help
    By mastern200 in forum C++ Programming
    Replies: 6
    Last Post: 01-15-2007, 01:51 PM
  3. Numbers adding together = bad
    By Zyk0tiK in forum C Programming
    Replies: 5
    Last Post: 12-04-2005, 03:37 PM
  4. Replies: 4
    Last Post: 11-16-2004, 06:29 AM
  5. Replies: 3
    Last Post: 07-24-2002, 08:46 AM

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