Jump to content




help with number gernerator


34 replies to this topic

#21 Doyle3694

  • Members
  • 815 posts

Posted 25 September 2012 - 06:31 PM

we can use 65535... it's all the numbers that can be located on 16 bits, but the lag reason is more of a reason I understand...

#22 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 25 September 2012 - 06:36 PM

We CAN'T use math.random(1,65535) for the main reason that you would be calculating a random number between 1 and 65,535. That means it has a very small chance of hitting any one of the 16 useable numbers for the bundled cable output. So the program would be waiting quite a long time before it would hit RANDOMLY on one of those 16 very specific numbers. I challenge you to create a program that would print an output anytime math.random(1,65535) would hit on any one of the numbers used for the Redstone API. It would take a long time to reach any of those numbers.

#23 Anonomit

  • Members
  • 67 posts

Posted 25 September 2012 - 06:44 PM

View PostCranium, on 25 September 2012 - 06:26 PM, said:

View PostDoyle3694, on 25 September 2012 - 06:15 PM, said:

Why don't you:?

rs.setBundledOutput("back", math.random(1, 65535))
Because for two reasons:
  • 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 Doyle3694

  • Members
  • 815 posts

Posted 25 September 2012 - 06:52 PM

View PostCranium, on 25 September 2012 - 06:36 PM, said:

We CAN'T use math.random(1,65535) for the main reason that you would be calculating a random number between 1 and 65,535. That means it has a very small chance of hitting any one of the 16 useable numbers for the bundled cable output. So the program would be waiting quite a long time before it would hit RANDOMLY on one of those 16 very specific numbers. I challenge you to create a program that would print an output anytime math.random(1,65535) would hit on any one of the numbers used for the Redstone API. It would take a long time to reach any of those numbers.

well no, 65535 would be all colors.

#25 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 25 September 2012 - 07:01 PM

View PostDoyle3694, on 25 September 2012 - 06:52 PM, said:

well no, 65535 would be all colors.
Did you read what I wrote? Yes, 65535 would be all colors. BUT, with math.random, it would randomly select ALL NUMBERS WITHIN THAT RANGE. You would have next to no chance of finding every combination of colors. If I did my math right, there are a total of 256 possible combinations of colors within 65,535 DIFFERENT numbers. That is a 0.39% chance of hitting upon one of those specific combinations each random pass. Adding such a huge amount to math.random is never a good idea unles you have a very good reason to. For this application, we would instead want something like several iterations of math.random(1,16). Theb compare the results with existing color numbers or codes with the redstone API. Much easier. Period.

#26 GopherAtl

  • Members
  • 888 posts

Posted 25 September 2012 - 07:10 PM

cranium, calm down and back up. You're right but you're not making it at all clear why you're right, I had to go read page one to see the original problem to understand myself.

Doyle, They don't want random values for all 16 wires, they want to randomly activate exactly one of the 16 wires.

#27 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 25 September 2012 - 07:21 PM

Sorry, got a little heated there, huh?
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
end
This 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 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 25 September 2012 - 10:25 PM

Well, if you only want one color at a time, then Cranium is (kind of) right.
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 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 25 September 2012 - 10:28 PM

Causes lag for me.....something wrong with my setup then?

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 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 25 September 2012 - 10:38 PM

View PostCranium, on 25 September 2012 - 10:28 PM, said:

Causes lag for me.....something wrong with my setup then?

Edit: Maybe it's becuase I have only used large numbers in an infinite loop.
I just tested, it works fine. The only thing is that it uses 32-bit signed integers, so the maximum value is 2147483647. But it generated 100000 numbers in ~0.05 seconds. Used this code:
local t0 = os.clock()
for i = 1, 100000 do
  math.random(2147483647)
end
print(os.clock() - t0)


#31 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 25 September 2012 - 10:59 PM

I'm not at my minecraft or CCemu right now, but I have never done it with large numbers in a for loop. Just in a while true do loop.
while true do
local number = math.random(1,2147483647)
print(number)
sleep(0) --if I change it, then it goes too slow.
end
Not 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... :P/>

#32 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 25 September 2012 - 11:32 PM

View PostCranium, on 25 September 2012 - 10:59 PM, said:

I'm not at my minecraft or CCemu right now, but I have never done it with large numbers in a for loop. Just in a while true do loop.
while true do
local number = math.random(1,2147483647)
print(number)
sleep(0) --if I change it, then it goes too slow.
end
Not 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... :P/>
Well, that doesn't cause lag neither. But if it did, the problem is not the math.random, it's the loop. It's an infinite loop that yields for almost no time, it will obviously cause some lag.

#33 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 25 September 2012 - 11:33 PM

Then I'm afraid I am being a stupid programmer... :P/>

#34 Doyle3694

  • Members
  • 815 posts

Posted 26 September 2012 - 06:31 PM

So math.random (1, 65535) works then? or doesn't it? I'm abit confused, I'm just some average programer, no pro by all means.

#35 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 26 September 2012 - 06:38 PM

Yes, it works.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users