generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Previous Thread :: Next Thread 
championbuilder is online. championbuilder
Joined: 20 Jan 2009
Total Posts: 13544
27 Mar 2015 03:57 PM
Like say you want every number between 1 and 100, but you also want them in random order. So instead of it chosing the same number twice, or going from 100 to 1, or 1 to 100, it goes like.

63
56
12
76
1
84
25
53
92
47

Without getting the same number twice, and carries on until it's got all 100 numbers.
Report Abuse
K7Q is online. K7Q
Joined: 23 Mar 2013
Total Posts: 5546
27 Mar 2015 03:58 PM
i = math.random(1,1000)
print(i)
Report Abuse
Argelius is not online. Argelius
Joined: 19 Jul 2010
Total Posts: 4047
27 Mar 2015 03:58 PM
You'd have to create a table of numbers from 1 to 100, use math.random(1,#tab) and remove them after they are used
Report Abuse
championbuilder is online. championbuilder
Joined: 20 Jan 2009
Total Posts: 13544
27 Mar 2015 03:59 PM
No that can still get the same number twice.
Report Abuse
championbuilder is online. championbuilder
Joined: 20 Jan 2009
Total Posts: 13544
27 Mar 2015 03:59 PM
@Arg, can I have an example? Isn't there a easier way to do this?
Report Abuse
Argelius is not online. Argelius
Joined: 19 Jul 2010
Total Posts: 4047
27 Mar 2015 04:00 PM
How would it get the same number twice if the number is the value in the table and not the result of math.random(1,#tab)
Report Abuse
MrJoeyJoeJoey is not online. MrJoeyJoeJoey
Joined: 20 Aug 2011
Total Posts: 20787
27 Mar 2015 04:03 PM
local tab={1,2,3,4,5}
local place=math.random(#tab)
local num=tab[place]
table.remove(tab,place)
Report Abuse
LegitimatlyMe is not online. LegitimatlyMe
Joined: 30 Oct 2013
Total Posts: 1108
27 Mar 2015 04:03 PM
local tabelofnums = {}

function generate()
return math.random(1,100)
end

for i = 1,100 do
wait()
function check()
local rannum = generate()
if tabelofnums[tostring(rannum)] then
wait()
check()
else
print(rannum)
return
end
end
end
Report Abuse
Argelius is not online. Argelius
Joined: 19 Jul 2010
Total Posts: 4047
27 Mar 2015 04:03 PM
tab = {}
for i = 1, 100 do
tab[i] = i
end

for i = 1, 100 do
r = math.random(1,#tab)
print(tab[r])
table.remove(tab,r)
end
Report Abuse
MrJoeyJoeJoey is not online. MrJoeyJoeJoey
Joined: 20 Aug 2011
Total Posts: 20787
27 Mar 2015 04:04 PM
Mines better

also before the script instead of inserting yourself you can do

for i=1,100 do
table.insert(tab,i)
end
Report Abuse
MrJoeyJoeJoey is not online. MrJoeyJoeJoey
Joined: 20 Aug 2011
Total Posts: 20787
27 Mar 2015 04:04 PM
@legit
Report Abuse
championbuilder is online. championbuilder
Joined: 20 Jan 2009
Total Posts: 13544
27 Mar 2015 04:07 PM
I wuv you MrJoeyJoeJoey
Report Abuse
LegitimatlyMe is not online. LegitimatlyMe
Joined: 30 Oct 2013
Total Posts: 1108
27 Mar 2015 04:08 PM
Pretty inefficient, amirite?
Report Abuse
MrJoeyJoeJoey is not online. MrJoeyJoeJoey
Joined: 20 Aug 2011
Total Posts: 20787
27 Mar 2015 04:14 PM
i wuv u to
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
27 Mar 2015 04:14 PM
function Random(n1, n2)
local Nums = {}
for i = n1, n2 do
table.insert(Nums, i)
end

return function()
local ind = math.random(1, #Nums)
local val = Nums[ind]
table.remove(Nums, ind)
return val
end
end


local RandomNumber = Random(1, 100)

for i = 1,100 do
print(RandomNumber())
end
Report Abuse
CrescentJade is not online. CrescentJade
Joined: 07 Jul 2010
Total Posts: 5494
27 Mar 2015 04:25 PM
t={}

function printNumbers(tab)
for i,v in next,tab do
return type(v)=="number" and print(v) and rawset(tab,i,nil) or nil
end
end

for i = 1,100 do
rawset(t,i,i)
end

printNumbers(t)

Report Abuse
CrescentJade is not online. CrescentJade
Joined: 07 Jul 2010
Total Posts: 5494
27 Mar 2015 04:27 PM
nvm dont use that, i dont feel like fixing it.
Report Abuse
Eternalfireeater is not online. Eternalfireeater
Joined: 01 May 2011
Total Posts: 10027
27 Mar 2015 04:39 PM
minimum=1
maximum=10
iterations=0

values={}

for i = minimum,maximum do
values[#values+1]=i
end

randomVals={}

function wasChosen(val)
for i,v in pairs(randomVals) do
if v == val then
return true
end
end
end

repeat

print(iterations)

local c = math.random(minimum,maximum)

if not wasChosen(c) then
randomVals[#randomVals+1] = c
end

wait()
iterations=iterations+1

until #randomVals==#values

print('done',iterations)
Report Abuse
Eternalfireeater is not online. Eternalfireeater
Joined: 01 May 2011
Total Posts: 10027
27 Mar 2015 04:43 PM
Actually, 100 iterations without yielding won't crash your script - only freeze up for a moment. So use this instead:

minimum=1
maximum=10
iterations=0

values={}

for i = minimum,maximum do
values[#values+1]=i
end

randomVals={}

function wasChosen(val)
for i,v in pairs(randomVals) do
if v == val then
return true
end
end
end

repeat

print(iterations)

local c = math.random(minimum,maximum)

if not wasChosen(c) then
randomVals[#randomVals+1] = c
end

iterations=iterations+1

until #randomVals==#values

print('done',iterations)
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
27 Mar 2015 04:44 PM
Mine is the best by far.

It uses closures and returns a function to be frkn awesome.
Report Abuse
CrescentJade is not online. CrescentJade
Joined: 07 Jul 2010
Total Posts: 5494
27 Mar 2015 04:47 PM
t={}

function printNumbers(tab)
local i=math.random(1,100)
for _=1,#tab do
repeat i=math.random(1,100) until not not tab[i]==true
print(tab[i])
rawset(tab,tab[i],nil)
end
end

for i = 1,100 do
rawset(t,i,i)
end

printNumbers(t)
Report Abuse
CrescentJade is not online. CrescentJade
Joined: 07 Jul 2010
Total Posts: 5494
27 Mar 2015 04:53 PM
t={}
math.randomseed(os.time())

function printNumbersInRandomOrder(tab)
local i=math.random(1,100)
for _=1,#tab do
repeat i=math.random(1,100) until not not tab[i]==true
print(tab[i])
rawset(tab,tab[i],nil)
end
end

for i = 1,100 do
rawset(t,i,i)
end

printNumbersInRandomOrder(t)

Tis more unpredictable without making a function for it.
Report Abuse
Seranok is not online. Seranok
Joined: 12 Dec 2009
Total Posts: 11083
27 Mar 2015 04:57 PM
Lots of terrible solutions here. Here's an O(N) solution.

function randomizeTable(t)
for i = 1, #t - 1 do
local r = math.random(i, #t)
t[i], t[r] = t[r] ,t[i]
end
end

local t = {}
for i = 1, 100 do
t[i] = i
end
randomizeTable(t)

for i = 1, 100 do
print(t[i])
end
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
27 Mar 2015 05:00 PM
@Above, Yes, if you want to be traditional and efficient. I was aiming more for the clever and cool looking code lol.
Report Abuse
CrescentJade is not online. CrescentJade
Joined: 07 Jul 2010
Total Posts: 5494
27 Mar 2015 05:00 PM
How is your solution any better?
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image