Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random picked numbers

Status
Not open for further replies.

giahan

Programmer
Sep 1, 2000
139
US
I work on an application that requires the computer to pick out a number less than 100 every 5 minutes in 10 hrs (120 numbers are produced in 10 hrs). In those 120 numbers, the computer will pick out 12 numbers randomly at the time the computer generate the number. The application will run everyday from 7AM to 5PM.
I am stuck on how to constraint 12 numbers. Any help will be really appreciated.
Thanks a lot.


GH
 
So what happens every 5 minutes?
- nothing
- something
- depends on the random value generated.

You seem to be suggesting that you may as well generate 120 numbers at 5pm, and go from there.

> at the time the computer generate the number
What does this mean?


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
- every 5 minutes, the computer pick out a random number (which is <100, ie. 70), it prints out the number it picks.
- After it generate this number (ie. 70), it also tells that this number is picked to be in the sample.
- There will be 120 numbers get created from 7AM to 5PM. Out of 120 numbers, there will be only 12 numbers get pick to be the sample.
Here is what it should output:
- At 7:00 AM, number picked 70, count #1, number is picked. Sample #1
- At 7:05 AM, number picked 25, count #2, number is NOT picked.
- At 7:10 AM, number picked 30, count #3, number is NOT picked.
- At 7:15 AM, number picked 32, count #4, number is NOT picked.
.......
- At 10:45 AM, number picked 99, count #xx, number is picked. Sample #2
- At 10:50 AM, number picked 22, count #xx+1, number is picked. Sample #3
.....
- At 4:20 AM, number picked 55, count #1yy, number is picked. Sample #12
.....
.....
- At 5:00 PM, number picked 21, count #120, number is NOT picked.

As a note, all the time that it picks out the sample has to be random. The output has to be printed out has the number gets generated. The sample per 10-hr day has to be 12.

I get stuck at the part that how to pick out 12 numbers randomly in the 120-count set.
Thanks for helping.




GH
 
At the start of the day, 7:00 AM, generate a list of 12 times between 7:00 and 17:00 at which the number is to be picked.

One way would be to put all 120 times into an array, shuffle that array, and then pick the first 12 entries. This guarantees that you'll always get unique times.


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
It seems it's rather an odd and artificial specification in the post #3. If we have a good (pseudo-) random numbers generator (with a proper seed initialization) or (especially) hardware random generator, no matter what's selected sequence. If we have a bad one, we can't improve random sequence quality with shaman dances around selecting order...

Apropos, shuffled random sequence is not better than original one in the theory of probability sense.

If you want true (or near;) random choices, you can't use pseudo-random generators at all. In that case you need more complex approach with disk rotation speed sensors etc (at least;). It's another story.
 
True. The problem here is the number generated and picked have to be at realtime. i can't pre-generate them.


GH
 
Why can you not pre-generate them? A number that's random at 4:45 is going to be just as random at 8:20.

Generate 12 random values at 8:00 and place them into an array... call it Value. Also at 8:00, generate another 12 random numbers between 1 and 120, also placed into an array... we'll call this one Time. Each value in Time corresponds to a 5-minute period.

Every 5 minutes, scan the Time array... if the current 5-minute period matches up with one listed in the Time array, grab a number from the Value array (or just generate the damn thing on the fly and forget about the Value array altogether).

None of your requirements make much sense, and it sounds like you're going around your elbow to get to you rear end. Why is so much complication needed for such a simple problem? There has to be a reason for the complication, be it a homework assignment that says you must do it this way or some requirement you're not letting us in on.
 
What happens if you get to say 16:45 and you've only generated two of your 12 samples?

You've run out of time slots to generate the required number of samples.

Planning the day in advance is going to be just as effective, and be far simpler to implement than the approach you're suggesting of incrementally fiddling with probabilities to ensure that you always get 12 samples in 10 hours.

Plus if you're calling standard [tt]rand()[/tt], then you'll get EXACTLY the same sequence if you call it 120 times in a loop at 07:00 as you would calling it once every 5 minutes through the day. Once you've set the seed, it's entirely deterministic in that waiting has no impact on the result it will return next.


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
The reason i try to avoid the pre-generate approach is to allow the future capability - which allow user to enter a number at 5-minute cycle, then the entry will either be picked as sample or not. I code is currently go with pre-generated approach but i think there is a better way, so i posted this question. I do not want to go back to the array in my future capability and delete the pre-generated value and replace the user-entered value in the array.

I already got the algorithm to check the last 12 times, which start at 16:00 and make sure that i do get no more than 12 samples that get picked.

Thanks a lot for your help.


GH
 
I don't see that you'd be "deleting" and "replacing" items in your predetermined array.

You can just say
Code:
Value[(TimeInMinutes-SevenAMInMinutes)/5]=somevaluetyped

Then at the end of the day, pick your 12 values.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top