ATTENTION

This 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

Go Back   FlexSim Community Forum > FlexSim Software > Q&A
Downloads

Q&A Using Flexsim and building models

  #1  
Old 07-30-2008
Xavier Jackson's Avatar
Xavier Jackson Xavier Jackson is offline
Flexsim User
 
Join Date: Jun 2008
Posts: 20
Downloads: 11
Uploads: 0
Thanks: 21
Thanked 4 Times in 3 Posts
Rep Power: 147
Xavier Jackson is on a distinguished road
Default 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  
Old 07-30-2008
AlanZhang's Avatar
AlanZhang AlanZhang is offline
Flexsim Super Moderator
 
Join Date: Aug 2007
Location: CA
Posts: 289
Downloads: 64
Uploads: 0
Thanks: 88
Thanked 91 Times in 47 Posts
Rep Power: 225
AlanZhang is a jewel in the roughAlanZhang is a jewel in the roughAlanZhang is a jewel in the rough
Default

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  
Old 07-30-2008
Alex Christensen Alex Christensen is offline
Flexsim Technical Support
 
Join Date: Nov 2007
Location: Provo, UT
Posts: 96
Downloads: 41
Uploads: 8
Thanks: 29
Thanked 141 Times in 56 Posts
Rep Power: 298
Alex Christensen is a splendid one to beholdAlex Christensen is a splendid one to beholdAlex Christensen is a splendid one to beholdAlex Christensen is a splendid one to beholdAlex Christensen is a splendid one to beholdAlex Christensen is a splendid one to beholdAlex Christensen is a splendid one to behold
Default

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
My code runs faster
The Following 2 Users Say Thank You to Alex Christensen For This Useful Post:
Xavier Jackson (07-30-2008)
  #4  
Old 07-30-2008
AlanZhang's Avatar
AlanZhang AlanZhang is offline
Flexsim Super Moderator
 
Join Date: Aug 2007
Location: CA
Posts: 289
Downloads: 64
Uploads: 0
Thanks: 88
Thanked 91 Times in 47 Posts
Rep Power: 225
AlanZhang is a jewel in the roughAlanZhang is a jewel in the roughAlanZhang is a jewel in the rough
Default

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


All times are GMT -6.
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2020, vBulletin Solutions Inc.
Copyright 1993-2018 FlexSim Software Products, Inc.