|
| 23 Feb 2014 08:09 AM |
Seeing as this subforum is a decaying piece of inorganic matter, I'd like to freshen things up with a challenge.
Challenge is : to write (in the shortest amount of characters) a function to randomly choose 0 or 1 favouring 1 twice as much as 0.
Example (of what would work but wouldn't win):
function randomizeTheNumbersPlease() local randomNumber = math.random(1,3) if randomNumber >= 2 then print(1) elseif randomNumber == 1 then print(0) end end |
|
|
| Report Abuse |
|
|
|
| 23 Feb 2014 08:10 AM |
| print(math.random(1,3)> 1 and 1 or 0) |
|
|
| Report Abuse |
|
|
|
| 23 Feb 2014 08:10 AM |
| print(math.min(math.random(0,2),1) |
|
|
| Report Abuse |
|
|
|
| 23 Feb 2014 08:11 AM |
print(math.random()>0.25 and 1 or 0)
AW MAN THIS ISN'T WHERE I PARKED MY CAR |
|
|
| Report Abuse |
|
|
| |
|
|
| 23 Feb 2014 08:13 AM |
:O Android noob detected
also he said to write a function
w=function()return math.random()>0.25 and 1 or 0 end
AW MAN THIS ISN'T WHERE I PARKED MY CAR |
|
|
| Report Abuse |
|
|
|
| 23 Feb 2014 08:17 AM |
| function()print(math.min(math.random(0,2),1)) |
|
|
| Report Abuse |
|
|
|
| 23 Feb 2014 08:18 AM |
| function()print(math.random()>0.25 and 1 or 0)end |
|
|
| Report Abuse |
|
|
|
| 23 Feb 2014 08:18 AM |
^ lol wrong code, sorry.
function()print(math.min(math.random(0,2),1))end |
|
|
| Report Abuse |
|
|
|
| 23 Feb 2014 08:23 AM |
| function()print(math.ceil(math.random()-1/3))end |
|
|
| Report Abuse |
|
|
|
| 23 Feb 2014 08:24 AM |
| function()print(math.abs(math.random(-1,1))end |
|
|
| Report Abuse |
|
|
| |
|
|
| 23 Feb 2014 08:39 AM |
| function{}print({0,1,1}[math.random(1,3)])end |
|
|
| Report Abuse |
|
|
|
| 23 Feb 2014 08:42 AM |
| just realized you can't do that |
|
|
| Report Abuse |
|
|
Dr01d3k4
|
  |
| Joined: 11 Oct 2007 |
| Total Posts: 17916 |
|
|
| 23 Feb 2014 08:43 AM |
@Android: You need ( ) around the { }.
@OP: You didn't say it had to be in Lua :P ->math.abs math.random -1,1 |
|
|
| Report Abuse |
|
|
| |
|
|
| 23 Feb 2014 08:48 AM |
| ^ reason why I use semicolons on Lua |
|
|
| Report Abuse |
|
|
Dr01d3k4
|
  |
| Joined: 11 Oct 2007 |
| Total Posts: 17916 |
|
|
| 23 Feb 2014 08:55 AM |
Aha!
local function a() return 1-(math.random(0,2)%2); end
local values = {[0] = 0; [1] = 0}; local total = 1000;
for i = 1, total, 1 do local x = a(); values[x] = values[x] + 1; end
for i = 0, 1, 1 do print(("%i: %3i %2.2f"):format(i, values[i], (100 / total) * values[i])); end
0: 331 33.10 1: 669 66.9
|
|
|
| Report Abuse |
|
|
|
| 23 Feb 2014 09:01 AM |
| droid, you really deserve your main group. ._. |
|
|
| Report Abuse |
|
|
| |
|
|
| 23 Feb 2014 09:18 AM |
| function()print(math.random(1,3)%2)end |
|
|
| Report Abuse |
|
|
|
| 23 Feb 2014 09:20 AM |
| I stand on the shoulders of giants. :P |
|
|
| Report Abuse |
|
|
|
| 23 Feb 2014 10:11 AM |
hacker detected
AW MAN THIS ISN'T WHERE I PARKED MY CAR |
|
|
| Report Abuse |
|
|
digpoe
|
  |
| Joined: 02 Nov 2008 |
| Total Posts: 9092 |
|
|
| 23 Feb 2014 10:26 AM |
print(1) Guaranteed to be random for most cases. |
|
|
| Report Abuse |
|
|
200AB
|
  |
| Joined: 24 Aug 2010 |
| Total Posts: 1604 |
|
|
| 23 Feb 2014 10:37 AM |
function() print(math.random(1,1000)*100/20-100%5)))) end
...did i win yet?? |
|
|
| Report Abuse |
|
|