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 » Scripting Helpers
Home Search
 

Re: Generating sequences of unique random numbers?

Previous Thread :: Next Thread 
froggodude is not online. froggodude
Joined: 22 Sep 2012
Total Posts: 1638
20 Jun 2014 10:26 PM
This is the algorithm I developed for generating sequences of unique random numbers
(none of them are to be repeated)
The only problem is that it seems to be slow and horribly inefficient.
Could anyone try to help me with it? I'm not asking you to revise the script, just to suggest new methods of achieving it or helping correct the script in words:

function randomSequence()
local function checkMatch(table1,table2)
for _,v in pairs(table1) do
for x,y in pairs(table2) do
if y == v and x~=#table2 then
return true
end
end
end
end
local function generateRan(role,grand)
while true do
wait()
local randomNum = math.random(1,game.Players.NumPlayers)
table.insert(role,randomNum)
table.insert(grand,randomNum)
if checkMatch(role,grand) then
table.remove(role,#role)
table.remove(grand,#grand)
else
break
end
end
return role
end
local grand = {}
local sequencePeanut = {}
local sequenceBanana = {}
local sequenceCats = {}
local sequenceMouse = {}
for i = 1,availablePeanutButter do
generateRan(sequencePeanut,grand)
end
for i = 1,availableBanana do
generateRan(sequenceBanana,grand)
end
for i = 1,availableCats do
generateRan(sequenceCats,grand)
end
generateRan(sequenceMouse,grand)
return sequencePeanut,sequenceBanana,sequenceCats,sequenceMouse
end

the "available" variables are global ones that I set in the main part of the script.
Report Abuse
swimguy777 is not online. swimguy777
Joined: 30 May 2009
Total Posts: 17092
20 Jun 2014 10:36 PM
SORTING AND BINARY SEARCHING :D
I took a class on this

local usedNumbers = {}

function binarySearch(num, low, high)
low = low and low or 1
high = high and high or #usedNumbers
local mid = (low + high)/2
if num == usedNumbers[mid] then return mid
elseif num > mid then return binarySearch(mid + 1, high)
elseif num < mid then return binarySearch(low, mid - 1)
end
end

function uniqueRandom()
while wait() do
local r = math.random(0,456789) --You can change the range
if not binarySearch(r) then return r end
end
end

-[::ƧѡÎḾḠΰῩ::]-[::Helper of Scripting and Writer of Wikis::]
Report Abuse
swimguy777 is not online. swimguy777
Joined: 30 May 2009
Total Posts: 17092
20 Jun 2014 10:37 PM
Oops, wait I forgot some stuff, hold on

-[::ƧѡÎḾḠΰῩ::]-[::Helper of Scripting and Writer of Wikis::]
Report Abuse
logo3578 is not online. logo3578
Joined: 01 Feb 2011
Total Posts: 18040
20 Jun 2014 10:38 PM
Hmm

It seems to be good, but if it's slow, I would try this script booster, makes it faster apparently

function sp = spdemo(arg1, arg2)
% Comparison of sparse vs full matrices
if (nargin ~= 2)
error('Give order and density');
return
end
S = sprandn(arg1,arg1,arg2);
F = full(S);
% Compare speed.

t0=cputime;
B = S * S;
stime=cputime-t0;

t0=cputime;
C = F * F;
ftime=cputime-t0;

sprintf('Order %d matrix, density %f: Full=%f, Sparse=%f', arg1, ...
arg2, ftime, stime)
Report Abuse
swimguy777 is not online. swimguy777
Joined: 30 May 2009
Total Posts: 17092
20 Jun 2014 10:42 PM
local usedNumbers = {}

function binarySearch(num, low, high)
low = low and low or 1
high = high and high or #usedNumbers
local mid = (low + high)/2
if num == usedNumbers[mid] then return mid
elseif num > mid then return binarySearch(num, mid + 1, high)
elseif num < mid then return binarySearch(num, low, mid - 1)
end
end

function uniqueRandom()
table.sort(usedNumbers)
while wait() do
local r = math.random(0,456789) --You can change the range
if not binarySearch(r) then
return r
end
end
end

-[::ƧѡÎḾḠΰῩ::]-[::Helper of Scripting and Writer of Wikis::]
Report Abuse
swimguy777 is not online. swimguy777
Joined: 30 May 2009
Total Posts: 17092
20 Jun 2014 10:56 PM
I gave it a customizeable range and added a checking system so it doesn't get stuck in an infinite loop.

WARNING: completely untested

local usedNumbers = {}

function binarySearch(num, low, high)
low = low and low or 1
high = high and high or #usedNumbers
if low > high then return nil end
local mid = (low + high)/2
if num == usedNumbers[mid] then return mid
elseif num > mid then return binarySearch(num, mid + 1, high)
elseif num < mid then return binarySearch(num, low, mid - 1)
end
end

function uniqueRandom(l, h) --Custom range
table.sort(usedNumbers)
local r
binaryL = binarySearch(l)
binaryH = binarySearch(h)
local check = nil
if binaryL ~= nil and binaryH ~= nil then
check = binaryH - binaryL
end
if check ~= nil and check == h - l then return nil end
while wait() do
r = math.random(l, h)
if not binarySearch(r) then
return r
end
end
end

-[::ƧѡÎḾḠΰῩ::]-[::Helper of Scripting and Writer of Wikis::]
Report Abuse
froggodude is not online. froggodude
Joined: 22 Sep 2012
Total Posts: 1638
20 Jun 2014 11:19 PM
Ok thanks swimguy! You are a lifesaver!

I'll test it out tomorrow, as I have to go to bed now. I'll let you know how it does.
Report Abuse
froggodude is not online. froggodude
Joined: 22 Sep 2012
Total Posts: 1638
21 Jun 2014 09:06 AM
It works perfectly. Thank you so much swimguy!
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
21 Jun 2014 09:08 AM
I've used this before, it only gets slow if you've been using it FOREVER.

ns = {}
done = false
repeat
repeat
movingon = true
n = math.random(1, 99999)
nn = math.random(1, 99999)
for i = 1,#ns do
if ns[i] == n or ns[i] == nn then
movingon = false
end
end
until movingon == true and n ~= nn
print("".. n .." ".. nn .."")
table.insert(ns, n)
table.insert(ns, nn)
a = 0
for i = 1,#ns do
a = a + 1
end
until done == true
Report Abuse
swimguy777 is not online. swimguy777
Joined: 30 May 2009
Total Posts: 17092
21 Jun 2014 11:18 AM
If you want to learn more about the method I chose, look up "Binary Search" or "Searching algorithms"

And if you want to speed it up even more you can implement your own sorting algorithm

-[::ƧѡÎḾḠΰῩ::]-[::Helper of Scripting and Writer of Wikis::]
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • 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