help with number gernerator
#21
Posted 25 September 2012 - 06:31 PM
#22
Posted 25 September 2012 - 06:36 PM
#23
Posted 25 September 2012 - 06:44 PM
Cranium, on 25 September 2012 - 06:26 PM, said:
Doyle3694, on 25 September 2012 - 06:15 PM, said:
rs.setBundledOutput("back", math.random(1, 65535))
- math.random will generate every possible number between 1 and 65535, and there are only 16 that we can use.
- Try running a random program that will calculate any number within that parameter, and you will lag the computer to hell.
Also, I want to display the random number on a monitor. I did this by calling a function that returns a string that's four characters long. This way, the number always takes up the same amount of space on the screen, so long as the number chosen is below 10 and not negative.
#24
Posted 25 September 2012 - 06:52 PM
Cranium, on 25 September 2012 - 06:36 PM, said:
well no, 65535 would be all colors.
#25
Posted 25 September 2012 - 07:01 PM
Doyle3694, on 25 September 2012 - 06:52 PM, said:
#26
Posted 25 September 2012 - 07:10 PM
Doyle, They don't want random values for all 16 wires, they want to randomly activate exactly one of the 16 wires.
#27
Posted 25 September 2012 - 07:21 PM
But yeah, Doyle, what we want is this:
local function colorConvert() local random = math.random(1,16) if random > 1 then return 2^(random - 2) else return 1 endThis makes the random amount from 1 to 16, and instantly converts it to the appropriate color code in decimal format.
It would be called back like this:
rs.setBundledOutput("side",colorConvert())That would randomly select one of 16 colors, and set it to that.
#28
Posted 25 September 2012 - 10:25 PM
But what Doyle said is correct. Colors in bundled cables are handled in 16 bits (1 bit for each color -> 16 colors), so when a color is on the bit is 1, when it's off it's 0. Now when you have more that one color on, there will be more than one 1 bit. So the number of possible combinations is 2^16, wich is 65536 (from 0 to 65535). Now if you generate a random number between 1 and 65535, you'll get a random combination of colors.
If you want to use the 16 colors, this way is a lot easier.
Also, who says that generating a high random number will cause lag? Try using math.random(1, 4294967296), it will take no time.
#29
Posted 25 September 2012 - 10:28 PM
Edit: Maybe it's becuase I have only used large numbers in an infinite loop.
Edited by Cranium, 25 September 2012 - 10:28 PM.
#30
Posted 25 September 2012 - 10:38 PM
Cranium, on 25 September 2012 - 10:28 PM, said:
Edit: Maybe it's becuase I have only used large numbers in an infinite loop.
local t0 = os.clock() for i = 1, 100000 do math.random(2147483647) end print(os.clock() - t0)
#31
Posted 25 September 2012 - 10:59 PM
while true do local number = math.random(1,2147483647) print(number) sleep(0) --if I change it, then it goes too slow. endNot sure if this is a bad idea, but that's an example I have used before. Lag city.....
Adding sleep(1) fixes it though...so maybe I am just being a stupid programmer... />
#32
Posted 25 September 2012 - 11:32 PM
Cranium, on 25 September 2012 - 10:59 PM, said:
while true do local number = math.random(1,2147483647) print(number) sleep(0) --if I change it, then it goes too slow. endNot sure if this is a bad idea, but that's an example I have used before. Lag city.....
Adding sleep(1) fixes it though...so maybe I am just being a stupid programmer... />
#33
Posted 25 September 2012 - 11:33 PM
#34
Posted 26 September 2012 - 06:31 PM
#35
Posted 26 September 2012 - 06:38 PM
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users