Generating different randoms valid for a day on different independent devices?
Let me describe the system. There are several mobile devices, each independent from each other, and they are generating content for the same record id.
I want to avoid generating the same content for the same record on different devices, for this I though I would use a random and make it so too cluster the content pool based on these randoms.Suppose you have choices from 1 to 100.
Day 1
- Device#1 will choose for the record#33 between 1-10
- Device#2 will choose for the record#33 between 40-50
- Device#3 will choose for the record#33 between 50-60
- Device#1 will choose for the record#55 between 40-50
- Device#2 will choose for the record#55 between 1-10
Device#3 will choose for the record#55 between 10-20
Device#1 will choose for the record#11 between 1-10
- D开发者_JAVA百科evice#2 will choose for the record#22 between 1-10
- Device#3 will choose for the record#99 between 1-10
Day 2
- Device#1 will choose for the record#33 between 90-100
- Device#2 will choose for the record#33 between 1-10
- Device#3 will choose for the record#33 between 50-60
They don't have access to a central server.
Data available for each of them:- IMEI (unique per mobile)
- Date of today (same on all devices)
- Record id (same on all devices)
What do you think, how is it possible?
ps. tags can be edited
I agree with AakashM and Frustrated. First can you guarantee that the number of devices is greater than the range of generated data? In fact to be "Random" you really need to have twice the generated content as devices, otherwise at least one device will have to "randomly choose" between one choice. That stated since the IMEI is the only unique data based on the device you will have to use that in your algorithm. The other problem (I suspect) is that the device IDs are not necessarily evenly distributed so any potential mapping of a device ID to a data block would potentially lead to overlap (unless you have a very large range of data to select from) or the values generated would not be evenly distributed across the pool of devices.
That said here is a rough algorithm:
- Create a mapping of the IMEI to an integer (i.e. some kind of hash)
- Rotate that integer based on the date and the size of possible values generated in step
- Map the number resultant from 2 to a range of data values based on the ratio of IMEIs to value range created by step #1.
- Use the standard random number to select something from that range
A simple example using the IMEI as a single byte and a data range of 0->2559:
- Mapping is direct based on IMEI, so base = IMEI
- Date translation is based on #of days since epoch. For this example say 700 then base = (9(original base: Mapped IMEI)+700(# of days)) % 255(range of mapped IMEI value) = 199
- Data range is 10 times mapped value range, so each block may use 10 values, resultant data range from 2 becomes 1990 ->1999 (inclusive).
- Select a standard random value in the block data range (i.e. 4, as chosen by fair dice roll) value = 1990+4=1994.
Finally if you want the ranges to differ based on the record you could use that as a second offset as was done in step #2.
If each device has a unique DeviceID (maybe based on the hardware serial number or something) you could use that instead of these random number schemes. Would keep things much simpler.
精彩评论