reset password
Author Message
giraid
Posts: 39
Posted 15:20 Jan 12, 2015 |

I have a question about the last problem for lab1.

Because we use time as srand, generating a large amount of numbers in a very short period of time yield very similar results, so I wrote something to delay each cycle of the loop. The randomness was improved but the processing time was increased as well. Does anyone have better solution to increase randomness without sacrificing speed?

jwarren6
Posts: 56
Posted 15:44 Jan 12, 2015 |

Not unless you use an external library. Whatever you did to cause a delay is necessary in order to improve randomness after the initial seed. For those that don't know, after using time as a seed, you get a new random number ONLY ONCE PER SECOND. I output every hit/miss from all players and announce each round's winner - that's my delay in order to improve randomness.

Last edited by jwarren6 at 15:46 Jan 12, 2015.
kscasado
Posts: 19
Posted 16:01 Jan 12, 2015 |

If you look online there are improvements on getting more random numbers out of the rand function. Just google: c++ random algorithim improvement and pick one. However with the standard random and looping it 1000 times the standard random should give you a pretty good solution. Using the improved algorithims only changed 3-4% for me.

raywu64
Posts: 44
Posted 16:13 Jan 12, 2015 |

I had a similar situation and I did import Sleep function from the library windows.h. But a simpler solution is to seed your random generator at the start of main.

If you make a new generator and feed it the same time, when you call Rand(), it's always going to give you the first random is that "list". For example, if your code looks something like this:

getRandom() {

srand(time(null));

return rand()%3

}

the list of random numbers you get is the same every second (I think). And what you get is the first number of that "list. 

tl;dr put srand() at the start of main and you'll get no issues.

 

jpascua
Posts: 197
Posted 19:20 Jan 12, 2015 |
raywu64 wrote:

I had a similar situation and I did import Sleep function from the library windows.h. But a simpler solution is to seed your random generator at the start of main.

If you make a new generator and feed it the same time, when you call Rand(), it's always going to give you the first random is that "list". For example, if your code looks something like this:

getRandom() {

srand(time(null));

return rand()%3

}

the list of random numbers you get is the same every second (I think). And what you get is the first number of that "list. 

tl;dr put srand() at the start of main and you'll get no issues.

 

I noticed this happening when debugging my program. However, I managed to solve it by placing it on main as you guys stated.

However, I thought we'd still be able to get random numbers even if we declare srand(time(null)); in the same function as rand(). I mean every time we call the getRandom(); function won't time(null) always be entering a different time value? If that makes any sense.

giraid
Posts: 39
Posted 20:03 Jan 12, 2015 |

I tried the recommended method of putting srand in main, it was able to produce numbers that are more random in a single run, but after running it a few times, the final results for each of those times were very similar. So I don't think the overall randomness was improved much.

jpascua
Posts: 197
Posted 20:11 Jan 12, 2015 |
giraid wrote:

I tried the recommended method of putting srand in main, it was able to produce numbers that are more random in a single run, but after running it a few times, the final results for each of those times were very similar. So I don't think the overall randomness was improved much.

Aren't the results supposed to stay consistent?

giraid
Posts: 39
Posted 20:29 Jan 12, 2015 |
jpascua wrote:
giraid wrote:

I tried the recommended method of putting srand in main, it was able to produce numbers that are more random in a single run, but after running it a few times, the final results for each of those times were very similar. So I don't think the overall randomness was improved much.

Aren't the results supposed to stay consistent?

Come to think about it, you are probably right. Forgot we were running it for 100,000 times, that would satisfy the rule of large number, wouldn't it? (don't really know that much about probability) Thanks a lot for pointing that out, almost throw out a perfectly valid idea, and thanks, everyone else who recommended it.

giraid
Posts: 39
Posted 20:35 Jan 12, 2015 |

oh one more thing, after implementing srand in main, the results from the two strategies became quite similar, was this the case for everyone else?

vnimnualrat
Posts: 6
Posted 21:53 Jan 12, 2015 |
giraid wrote:

oh one more thing, after implementing srand in main, the results from the two strategies became quite similar, was this the case for everyone else?

No, the results for my two strategies are quite different.

raywu64
Posts: 44
Posted 11:32 Jan 13, 2015 |
jpascua wrote:
giraid wrote:

I tried the recommended method of putting srand in main, it was able to produce numbers that are more random in a single run, but after running it a few times, the final results for each of those times were very similar. So I don't think the overall randomness was improved much.

Aren't the results supposed to stay consistent?

Yes, the results should still be very similar. But, if you are getting the same random number (let's say every second) you are effectively reducing the sample size.