ATTENTIONThis FlexSim Community Forum is read-only. Please post any new questions, ideas, or discussions to our new community (we call it Answers) at https://answers.flexsim.com/. Our new Question & Answer site brings a modern, mobile-friendly interface and more focus on getting answers quickly. There are a few differences between how our new Q&A community works vs. a classic, threaded-conversation-style forum like the one below, so be sure to read our Answers Best Practices. |
flexsim.com |
#1
|
||||
|
||||
Non Repeating Random Numbers
Is there a way to select a fair number of random numbers and guarantee that there were no repeats and thus each number was selected once. I essentially need to shuffle the numbers 1-1800
__________________
Each morning after drudging through his morning routine the mild-mannered Xavier Jackson arrives at work and when he sits down at his computer it powers up and he becomes the great Flexavier Jacksim Optimizer Extraordinaire
|
#2
|
||||
|
||||
Hi Jackson,
The following code should be good for your purpose. You may try it in the Flexsim script window. You will see outputs in the output console. Code:
treenode tmpNode = node("/Tools/TEMP",model()); if(!objectexists(tmpNode)){ nodeinsertinto(node("/Tools",model())); setnodename(last(node("/Tools",model())),"TEMP"); tmpNode = last(node("/Tools",model())); } int maxNum = 10; for(int i=1;i<=maxNum;i++){ nodeinsertinto(tmpNode); nodeadddata(last(tmpNode), DATATYPE_NUMBER); setnodenum(last(tmpNode), i); } intarray rndNums = makearray(maxNum); int cnt = 1; while(content(tmpNode)!=0){ int chosenRank = duniform(1, maxNum-cnt+1); rndNums[cnt] = getnodenum(rank(tmpNode, chosenRank)); destroyobject(rank(tmpNode, chosenRank)); cnt++; } for(i=1;i<=maxNum;i++){ pd(rndNums[i]);pr(); }
__________________
Best, Alan |
The Following 2 Users Say Thank You to AlanZhang For This Useful Post: | ||
Xavier Jackson (07-30-2008) |
#3
|
|||
|
|||
I guess this is what happens when we both try to answer the same post with code. We both came up with something that works, but they work completely differently. Alan's method makes treenodes with sequential values, then adds their values randomly to an array as it deletes them from the tree. My method makes a sequential array, then swaps each one at least once to a random position:
Code:
int arraysize=1800; intarray myarray=makearray(arraysize); int x; for(x=1;x<=arraysize;x++){ myarray[x]=x; } // myarray is now 1,2,3,4,5... for(x=1;x<=arraysize;x++){pd(myarray[x]);pc(',');}pr();//print the array //swap each element with a random element at least once, (on average twice) int temp; int swapindex; for(x=1;x<=arraysize;x++){ swapindex=duniform(1,arraysize); temp=myarray[swapindex]; myarray[swapindex]=myarray[x]; myarray[x]=temp; } for(x=1;x<=arraysize;x++){pd(myarray[x]);pc(',');}pr();//print the array |
The Following 2 Users Say Thank You to Alex Christensen For This Useful Post: | ||
Xavier Jackson (07-30-2008) |
#4
|
||||
|
||||
Hi Alex,
You code is very interesting and use a complete different approach that I may never think of. And as you said, you code probably runs a bit faster. But I think my code is easier to understand. See I do not even need to put any comment into the code.
__________________
Best, Alan |
Thread | Thread Starter | Forum | Replies | Last Post |
Statistics: Random Number Streams | Tom David | Q&A | 19 | 08-12-2014 02:02 AM |
Repeat Random Streams | Cliff King | Q&A | 16 | 08-30-2012 10:49 AM |
real random numbers | Vinay Mehendiratta | Q&A | 1 | 07-15-2008 10:09 AM |
More random random number | Paul Dowling | Q&A | 6 | 06-01-2008 07:30 PM |