Thread: question about "random" and a calculator

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    7

    question about "random" and a calculator

    hello all, im new to C++ so sorry if these questions are kind of noob (wich they probably are )

    im trying to learn C++ by folowing the tutorials at this site, and so far it works pretty good.
    but after doing lesson 1 (or 2 maybe) i made a really basic calculator, but i cant get it to work fully.
    i do not know how to get C++ to use an operator that was put in by the user, so i tried simply NR3= NR1 OP1 NR2 where NR1 is the first input, OP1 the operator input, and NR3 is the second number input.

    i have added my complete calculator C++ code.

    how would i make this work?

    second, i tried at lesson 3 the random option, but i cant get it really random, its semi random, in wich i get the same numbers over and over again.
    i know computers aren't really capable of generating random numbers, but i think it should be a litle more random every time i open the program...
    i saw the rest of the lesson, im just curious about why it gives me every time the same number sequency...

    i have also added that C++ code in this post.

    thanks in advance for the answers!

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by godofal View Post
    second, i tried at lesson 3 the random option, but i cant get it really random, its semi random, in wich i get the same numbers over and over again.
    i know computers aren't really capable of generating random numbers, but i think it should be a litle more random every time i open the program...
    i saw the rest of the lesson, im just curious about why it gives me every time the same number sequency...
    That's how a random number generator works:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main() {
    	int i;
    
    	//srand(time(NULL));
    
    	for (i=0;i<100;i++) 
    		printf("%d ",rand());
    
    	return 0;
    }
    This will produce the same pattern of 100 numbers every time unless you uncomment the "srand()" line -- this seeds the Random Number Generator with a "random" value, meaning the sequence will always be different (since the time is never the same).

    You don't need to use srand() more than once, at the beginning of the program, before you call rand(). Doing it more than once could actually result in less random numbers (tho it may not, depending on how you do it).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    An addition to MK27's post which might be important:
    rand() MIGHT NOT produce the same number sequence every time the program is started. And in fact, usually it doesn't, it usually changes after a reboot of the computer. And maybe even in other situations. So never depend on rand producing the same set of random numbers.

    In fact, calling rand without first calling srand is pretty much useless. Calling srand with a parameter that is constantly the same is pretty much useless as well. Always use something that changes over time.... Like time! I usually even XOR the time with the pid of the application shifted left a few times, just in case the application is ran twice in the same second.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by EVOEx
    Calling srand with a parameter that is constantly the same is pretty much useless as well.
    Unless your aim is to reproduce the sequence, but since the algorithm for rand() is implementation defined, that has limited value if you want to be portable.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by EVOEx View Post
    Calling srand with a parameter that is constantly the same is pretty much useless as well.
    Yes, that will be the most dumb thing to do.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by laserlight View Post
    but since the algorithm for rand() is implementation defined, that has limited value if you want to be portable.
    That was kinda part of the reason I consider it pretty much useless ;-).

    Is rand() even defined to produce the same sequence, given a seed, on the same platform?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by EVOEx
    Is rand() even defined to produce the same sequence, given a seed, on the same platform?
    Yes, on the same standard library implementation. If rand() is called before srand(), it will be as if srand(1) was called first.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by laserlight View Post
    Yes, on the same standard library implementation. If rand() is called before srand(), it will be as if srand(1) was called first.
    That's odd, I could swear the first value rand() returned to me once was changed after a reboot... Guess I remember wrongly then.
    (Not being sarcastic, it was years ago, I might have ........ed up somewhere :P)

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    7
    hmm
    well, il look into that srand, but that still doesnt explain (to me at least) why rand doesnt give new random numbers every time...

    and how about that calculator program? anyone knows how to solve it?

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by godofal View Post
    hmm
    well, il look into that srand, but that still doesnt explain (to me at least) why rand doesnt give new random numbers every time...
    The point of rand is to not give you different numbers each time. (Most implementations of rand just have a "stream" of random numbers that doesn't ever change. If you start in the same place you will get the same numbers in the same order. If nothing else, this allows you to test programs using rand to make sure they do what they're supposed to.)
    Quote Originally Posted by godofal View Post
    and how about that calculator program? anyone knows how to solve it?
    You had a question about the calculator program? What was it?

  11. #11
    Registered User
    Join Date
    Mar 2010
    Posts
    7
    so rand is just for testing?

    and the question for that calculator: first post, i tried to make a simple calculator, but ran into problems with the actual calulating bit, since codeblocks doesnt allow NR3 = NR1 OP1 NR2 (NR1 and NR2 are inputs, as is OP1, wich should be something like +,-,* etc) as a calculation.

    and i asked how i would make it work...

  12. #12
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    so rand is just for testing?
    There is nothing wrong with using `std::rand' so long as it is used properly.* These guys are just explaining what you are seeing.

    Any PRNG (RNG) set to the same exact state will produce the same output. (This is even true for many cryptographically secure CSPRNG; it just happens that most CSPRNG implementations use entropy from the environment that isn't trivially stored for later replication.) A PRNG (RNG) basically samples bits--reads the bits in order--of some huge number. (This is the PRNG algorithms period.) It is perfectly natural for any PRNG to produce the subsequence many times in one pass over the entire period. (You are sampling a number with millions/billions/trillions of bits a few bits at a time. You are seeing the forest through a very small window.) It is perfectly natural for any RPNG to produce the same values many times in a row. (Even in sampling the digits of a transcendental number you are certainly to find repetitions.)

    The trick is not using a PRNG (RNG) as a CSPRNG or expecting either of them to be truly random. They only need to be indistinguishable from truly random under statistical analysis and, for CSPRNG algorithms, be difficult to "attack". (The reason most people despise `std::rand' is because of the common implementations; for many of them, you only need one generated value to determine the state of the generator.**)

    If you need a portable PRNG with great characteristics search around for `CMWC4096'. It has a massive period, is generally faster than other implementations, and you'd need at least 4096(*4097) generated values to determine the state of the generator.

    Soma

    * There have been some historical problems with `std::rand' implementations not fully mixing the current state causing some "bad" sequences, but most implementations you'll come across these days are fine.

    ** Being able to determine the state of the generator isn't a big deal for most applications--including a simple calculator.
    Last edited by phantomotap; 03-18-2010 at 05:27 AM.

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by godofal View Post
    hmm
    well, il look into that srand, but that still doesnt explain (to me at least) why rand doesnt give new random numbers every time...
    Because computers cannot generate truly "random" numbers. The random number generator generate a sequence of "pseudo-random" numbers. If the seed is the same, the sequence is always the same. If you do not seed it yourself, it defaults to 1. So the numbers are always the same.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  14. #14
    Registered User
    Join Date
    Mar 2010
    Posts
    7
    Quote Originally Posted by phantomotap View Post
    There is nothing wrong with using `std::rand' so long as it is used properly.* These guys are just explaining what you are seeing.

    Any PRNG (RNG) set to the same exact state will produce the same output. (This is even true for many cryptographically secure CSPRNG; it just happens that most CSPRNG implementations use entropy from the environment that isn't trivially stored for later replication.) A PRNG (RNG) basically samples bits--reads the bits in order--of some huge number. (This is the PRNG algorithms period.) It is perfectly natural for any PRNG to produce the subsequence many times in one pass over the entire period. (You are sampling a number with millions/billions/trillions of bits a few bits at a time. You are seeing the forest through a very small window.) It is perfectly natural for any RPNG to produce the same values many times in a row. (Even in sampling the digits of a transcendental number you are certainly to find repetitions.)

    The trick is not using a PRNG (RNG) as a CSPRNG or expecting either of them to be truly random. They only need to be indistinguishable from truly random under statistical analysis and, for CSPRNG algorithms, be difficult to "attack". (The reason most people despise `std::rand' is because of the common implementations; for many of them, you only need one generated value to determine the state of the generator.**)

    If you need a portable PRNG with great characteristics search around for `CMWC4096'. It has a massive period, is generally faster than other implementations, and you'd need at least 4096(*4097) generated values to determine the state of the generator.

    Soma

    * There have been some historical problems with `std::rand' implementations not fully mixing the current state causing some "bad" sequences, but most implementations you'll come across these days are fine.

    ** Being able to determine the state of the generator isn't a big deal for most applications--including a simple calculator.
    i didnt understand any of this, please keep in mind im still a complete newb at this
    can you explain in something a bit more plain english?

    oh, and i olmost forgot, id really like some answers about my calculator program XD
    Last edited by godofal; 03-18-2010 at 09:36 AM.

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by godofal View Post
    so rand is just for testing?

    and the question for that calculator: first post, i tried to make a simple calculator, but ran into problems with the actual calulating bit, since codeblocks doesnt allow NR3 = NR1 OP1 NR2 (NR1 and NR2 are inputs, as is OP1, wich should be something like +,-,* etc) as a calculation.

    and i asked how i would make it work...
    You need to parse the input yourself. If you see a +, do a +. If you see a -, do -. If you see ^, do pow(). But you need to act on the input.

Popular pages Recent additions subscribe to a feed