View Full Version : Math.RandomSeed() question
stickck
06-08-2006, 07:56 AM
i'm using this in a quiz.
Math.RandomSeed(nSeedVariable)
nRnd = Math.Random(1, Table.Count(BigQuiz));
lets say i have 100 questions in the quiz, will this code show the same question more than once in the quiz, or will it only come up the same number once?
another way to put it is, will question 1 be put into the quiz more than once. i've had a user say that everyonce in a while, they would get the same question in a single quiz but i havent been able to duplicate the problem.
any help would be great.
Chris
TJ_Tigger
06-08-2006, 08:54 AM
It is a random number with a range from 1 to whatever the table contains. This could generate 1,1,1,4,99,62 as your random number if you were to grab a random question from the same pool. Here is my suggestion on a possible solution.
Generate a random seed.
Generate a random number from one to the number of questions in the quiz.
Extract that question to a new table.
Remove from the old table.
repeat step two through 4
TmpQuiz = {}
Math.RandomSeed(nSeedVariable);
nrnd = Math.Random(1, Table.Count(BigQuiz));
--Table.Remove returns the data removed so I used that to insert
--into the temp quiz table. :)
Table.Insert(TmpQuiz, 1, Table.Remove(BigQuiz, nRnd));
This generates a random quiz from the original quiz without duplicating a question. Keep in mind that part of the process of generating the random non-repeating questions is that it removes the used question from the quiz. Before you use the table BigQuiz again make sure you pull from the source to refresh the table so it contains all the data.
Tigg
stickck
06-08-2006, 05:42 PM
you helped me with this code for combinding a group of quizzes and then selecting a random 100 questions and quizing on just those 100 questions.
i was kind of hopeing that the Math.Random would only select a number once if you select a start and stop point.
so what's the point of Math.RamdomSeed? the help file doesnt explain it much.
Thanks TIGGER!
Chris
stickck
06-08-2006, 06:02 PM
This is the script i already have. The part i highlighted in RED should be doing what you said. Am I overlooking something? The questions start off in a table called question_list, then get put into a table called BigQuiz, then it runs the Math.RandomSeed and in a loop it runs the Math.Random and put the selected question back into the table question_list. That should work right?
Chris
BigQuestions = 0
BigQuiz = {}
BigAnswer = {}
BigAns_A = {}
BigAns_B = {}
BigAns_C = {}
BigAns_D = {}
BigTorF = {}
tbSelected = ListBox.GetSelected(this);
if tbSelected then
strScript = ListBox.GetItemData(this, tbSelected[1]);
module = ListBox.GetItemText(this, tbSelected[1]);
Application.RunScriptFile(strScript);
BigQuestions = BigQuestions + question_max;
for ii,vv in question_list do
--insert each item at the end of the temporary table
Table.Insert(BigQuiz, Table.Count(BigQuiz) +1, vv);
end
for ii,vv in answer_list do
--insert each item at the end of the temporary table
Table.Insert(BigAnswer, Table.Count(BigAnswer) +1, vv);
end
for ii,vv in Ans_A do
--insert each item at the end of the temporary table
Table.Insert(BigAns_A, Table.Count(BigAns_A) +1, vv);
end
for ii,vv in Ans_B do
--insert each item at the end of the temporary table
Table.Insert(BigAns_B, Table.Count(BigAns_B) +1, vv);
end
for ii,vv in Ans_C do
--insert each item at the end of the temporary table
Table.Insert(BigAns_C, Table.Count(BigAns_C) +1, vv);
end
for ii,vv in Ans_D do
--insert each item at the end of the temporary table
Table.Insert(BigAns_D, Table.Count(BigAns_D) +1, vv);
end
for ii,vv in TorF do
--insert each item at the end of the temporary table
Table.Insert(BigTorF, Table.Count(BigTorF) +1, vv);
end
end
Math.RandomSeed(nSeedVariable)
question_list = {}
answer_list = {}
Ans_A = {}
Ans_B = {}
Ans_C = {}
Ans_D = {}
TorF={}
for Q = 1, question_max do
nRnd = Math.Random(1, Table.Count(BigQuiz));
--You could set the insert position to one or Table.Count(question_list)+1
--Which ever works for you, just make sure they are the same for all the
--tables.
Table.Insert(question_list, 1, BigQuiz[nRnd])
Table.Insert(answer_list, 1, BigAnswer[nRnd])
Table.Insert(Ans_A, 1, BigAns_A[nRnd])
Table.Insert(Ans_B, 1, BigAns_B[nRnd])
Table.Insert(Ans_C, 1, BigAns_C[nRnd])
Table.Insert(Ans_D, 1, BigAns_D[nRnd])
Table.Insert(TorF, 1, BigTorF[nRnd])
--Remove the question and answer from the original tables so it is not used again
Table.Remove(BigQuiz, nRnd)
Table.Remove(BigAnswer, nRnd)
Table.Remove(BigAns_A, nRnd)
Table.Remove(BigAns_B, nRnd)
Table.Remove(BigAns_C, nRnd)
Table.Remove(BigAns_D, nRnd)
Table.Remove(BigTorF, nRnd)
end
TJ_Tigger
06-08-2006, 07:03 PM
so what's the point of Math.RamdomSeed? the help file doesnt explain it much.
Math.RandomSeed is required to help the random function of windows become more random. The randomization function built into Windows does not always create a truely random number. I remember reading about someone at MIT who used a lavalamp and a PC camera to generate a more random number based on the image retreived from the camera.
With Math.RandomSeed you can provide a number (hopefully never the same seed number) that will help in providing a truely random number. When seeding the random function, use variables that are always increasing so you never end up with the same seed.
Tigg
TJ_Tigger
06-08-2006, 07:08 PM
That should work right?
From what I see yes, that is generating a random quiz. No two quizzes should look alike when built this way.
Here is the same code as above in red just a few less lines
for Q = 1, question_max do
nRnd = Math.Random(1, Table.Count(BigQuiz));
--You could set the insert position to one or Table.Count(question_list)+1
--Which ever works for you, just make sure they are the same for all the
--tables.
--the Table.Remove action removes the item from the list and returns it as a result
Table.Insert(question_list, 1, Table.Remove(BigQuiz, nRnd))
Table.Insert(answer_list, 1, Table.Remove(BigAnswer, nRnd))
Table.Insert(Ans_A, 1, Table.Remove(BigAns_A, nRnd))
Table.Insert(Ans_B, 1, Table.Remove(BigAns_B, nRnd))
Table.Insert(Ans_C, 1, Table.Remove(BigAns_C, nRnd))
Table.Insert(Ans_D, 1, Table.Remove(BigAns_D, nRnd))
Table.Insert(TorF, 1, Table.Remove(BigTorF, nRnd))
end
stickck
06-08-2006, 07:52 PM
Cool! thanks! after reading your first post in this thread, i figured i could combined the lines together Table.Insert(question_list, 1, Table.Remove(BigQuiz, nRnd))
thats the next thing i need to start doing on a normal basis. i write out everything and combinding the lines together like how you put it would a better way.
Thanks again Tigger!
Chris
TJ_Tigger
06-09-2006, 07:50 AM
The better way is the one that works for you. If it gets the job done then it is good. I often don't combine lines at first as I don't see that far ahead and often treat them as different steps.
Tigg
I often don't combine lines at first as I don't see that far ahead and often treat them as different steps.
This is the way most of the developers I've worked with operate. The first round of dev is usually 3-5 times as many lines as the final product and is loaded with hard coded values, long running control structures and other redundant stuff... just to get the thing working as desired.
Then through refactoring (usually several rounds) values get externalize, code gets consolidated into global functions (methods), control structures get optimized, commenting gets improved (if it existed at all), etc... etc...
Often times (especially with particularly anal developers) the refactoring take longer than the initial coding! I think one of the real signs of maturity in developer is if/when they can say they are done. Some are so anxious that they release buggy junk... others would never release at all if they weren't forced too... it's a real sweet spot for sure.
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.