# question about "random" and a calculator

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 03-16-2010
godofal
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 :D )

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.

• 03-16-2010
MK27
Quote:

Originally Posted by godofal
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).
• 03-16-2010
EVOEx
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.
• 03-16-2010
laserlight
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.
• 03-16-2010
MK27
Quote:

Originally Posted by EVOEx
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.
• 03-16-2010
EVOEx
Quote:

Originally Posted by laserlight
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?
• 03-16-2010
laserlight
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.
• 03-16-2010
EVOEx
Quote:

Originally Posted by laserlight
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)
• 03-17-2010
godofal
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?
• 03-17-2010
tabstop
Quote:

Originally Posted by godofal
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
and how about that calculator program? anyone knows how to solve it?

• 03-18-2010
godofal
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...
• 03-18-2010
phantomotap
Quote:

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.
• 03-18-2010
MK27
Quote:

Originally Posted by godofal
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.
• 03-18-2010
godofal
Quote:

Originally Posted by phantomotap
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
• 03-18-2010
tabstop
Quote:

Originally Posted by godofal
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.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last