srand() and Windows 10

I have recently made a till training program where it tells you what the total sale, asks you to type in the cash given by the customer and then tells you the change that needs to be given.

However, to come up with the random total sales each time I used the code below:
1
2
3
		srand(time(NULL));
		totalSale = rand() % 2000 + 1; //Sale up to £20.00
		totalSale /= 100.00;


So I used rand() to generate a new random number while using srand() to generate a new seed using time. I did include the header <time.h> and the program runs fine but the random numbers are not random, they just keep starting at a random number and each new random number keeps increasing. This program did work in Windows 7, when I upgraded to Windows 10 this happened.

Have I done something wrong or does making a new seed work differently in Windows 10? I am using VS 2015 as well.

For example here is a program run:

The Total Sale is: 17.33
Enter the ammount given: 2000
Amount given: 20
Change: 2.67

The Total Sale is: 17.39
Enter the ammount given: 2000
Amount given: 20
Change: 2.61

The Total Sale is: 17.49
Enter the ammount given: 2000
Amount given: 20
Change: 2.51

The Total Sale is: 17.56
Enter the ammount given: 2000
Amount given: 20
Change: 2.44

The Total Sale is: 17.59
Enter the ammount given: 2000
Amount given: 20
Change: 2.41

The Total Sale is: 17.72
Enter the ammount given:

As you can see when I put in £20 the next total sale just increases and is not random.

I really cannot see what is wrong here.
I don't think this ever worked as intended.
If you call srand(time(0)) inside a loop, you'll be constantly reseeding the PRNG with the current number of seconds since the epoch. If you do srand(time(0)); x = rand(); more than once per second, x will take the exact same value during that whole second.
You should only call srand() once at the start of the program.
Last edited on
1) Do not use rand() in modern C++.
2) time() is bad seed generator.
3) Even if you use rand, never seed it more than once in your whole program.

I see that between invocation of your function 7-10 seconds passes. Do you have menu when you select operation over and over manually to test this?
I see what I was doing wrong, I had:
1
2
3
                srand(time(NULL));
		totalSale = rand() % 2000 + 1; //Sale up to £20.00
		totalSale /= 100.00;

in a loop which was used to ask the user each time what the customer gave in cash after the total sale was displayed.

I had srand() in a loop, I then put srand() at the start of the program and everything seems to be fine now, I am getting fairly decent random numbers and not what I was getting before.

How come you are only supposed to seed the program once and then get random numbers, I thought you have to seed it each time to get random numbers because the random numbers where based on time as that could be deemed as something random?
Last edited on
The start time of a program is often an acceptable randomness source, since it exhibits fairly low correlation between consecutive runs. The current time exhibits high correlation at all times. In particular, the current second is invariably equal to the last second +1. Additionally, time(0) == truncated_to_seconds(now), and it's very often true that truncated_to_seconds(now) == truncated_to_seconds(now - t), where t is a quantity of time smaller than a second.

In other words, this:
1
2
3
4
5
6
srand(time(0));
x = rand();
srand(time(0));
y = rand();
srand(time(0));
z = rand();
Almost always assigns the same number to x, y, and z.
Last edited on
PRNG or pseudorandom number generators works by doing complex calculations on previous state. Numbers generated are usually uniform and unpredictable enouh. Seeding is setting initial state: number you will start from.

Time is not unpredictable: give me a minute with your program and I will get guaranteed £20.00 discount on first item.
Also it is best to warm-up generator a little. In your case call rand() once after you seed generator .
+helios Thanks for the good read and I understand now what went wrong thank you for explaining it to me.

+MiiNiPaa Ok and so right after I give the program a new seed, I should then call rand() directly after like so:
1
2
3
4
srand(time(0)); //Do once at the start of program
rand(); //Call once after sarand()

      //Then use rand to give my variables random numbers... 
Thanks Duoas, I will save that and read it later.
Topic archived. No new replies allowed.