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
|
|||
|
|||
Create a table with random values
Hi all,
I have a problem, where I dont know, how to realize. I want to fill a table (for example the table has 3 rows and 1 column) with 3 random values between 1 and 5. This is no problem, but I want, that while one value exists, that another value should be created. For example: Row-Number Value 1 5 2 2 3 5 I want, that row number 3 should be 1, 3 or 4. Does anybody know how to solve this problem? Thanks, Michael |
#2
|
|||
|
|||
Hi,
What you need to do is create a random number between 1 and 5, then check it against already created random numbers. If the number already exist create a new number and repeat the check, if the number does not exist save it. Lars-Olof |
#3
|
|||
|
|||
Hi Lars-Olof,
thanks for your fast reply. My problem is, to check it against. I have no idea how to do. I tried the following: intarray myarray = makearray(5); int CounterMyArray = 1; for (int iCounter = 1; iCounter <= 5; iCounter ++) { int iRandomValue = duniform(1,5); while (CounterMyArray <= 5) { if (myarray[CounterMyArray] == iRandomValue) {iRandomValue = duniform(1,5); CounterMyArray = 1;} else {CounterMyArray = CounterMyArray + 1;} } CounterMyArray = 1; But this small code doesn' work. I know, it is not an table, but I thought, that an Array could do the same... |
#4
|
|||
|
|||
Hi,
Try this code. Code:
intarray myarray = makearray(5); int found; for (int iCounter = 1; iCounter <= 5; iCounter ++) { found=1; while (found==1) { int iRandomValue = duniform(1,5); found=0; for (int index=1; index<=iCounter; index++) { if (myarray[index] == iRandomValue) found=1; } if (found==0) myarray[iCounter] = iRandomValue; } } |
The Following 2 Users Say Thank You to Lars-Olof Leven For This Useful Post: | ||
Tom David (08-05-2009) |
#5
|
|||
|
|||
Hi,
Here is another solution where I use GlobalTable. What I do is set the table size to number of random numbers I want, then I populate the table with values.
Code:
treenode table=reftable("Table1"); intarray myarray = makearray(5); settablesize(table,5,1,1,1); for (int index=1; index<=gettablerows(table); index++) settablenum(table,index,1,index); for (index=1; index<=5; index++) { int rows=gettablerows(table); int randValue=duniform(1,rows); myarray[index]=gettablenum(table,randValue,1); deletetablerow(table,randValue); } Lars-Olof |
The Following User Says Thank You to Lars-Olof Leven For This Useful Post: | ||
Brandon Peterson (08-05-2009) |
#6
|
|||
|
|||
Double Array_Value
Hi Lars-Olof,
first I want to say, thanks for your help. Two solutions in this short time is more than I hoped before. It helps me, to understand, what I have to do. The idea with an found variable is very helpful for my problem. I tried your first Code and first it works realy fine. But after a small time Flexsim crashed. Attached you see a Queue. OnReset I write the Array-Values into the OutputConsole. I press the Reset Button for maybe 3 to 5 times and then Flexsim crashed. Do you have an idea why? |
#7
|
|||
|
|||
Hi,
I have the same problem, for me Flexsim locks up after pressing 3 to 4 times on reset. When I added this code the problem disappeared: Code:
for (int index1=1; index1<=5; index1++) myarray[index1]=0; It looks like Flexsim reuse the array in the same session and what happens is that next time we call OnReset. The array already exist with values from the last time and when we do the check we are going in a loop that never ends. Therefor we need to put all the values in the array to 0 or another value that we never will be generating. The first code I posted will take longer and longer time to run if you increase the number of random number you want to generate. There is a bigger chance that you will going into a loop that never ends or take long time to finish. The second code will be never go in to a loop that never ends or take long time, but the draw back is that you need to use a table. Hope this help you. Lars-Olof Last edited by Lars-Olof Leven; 08-05-2009 at 08:04 AM. Reason: Missed to change index++ to index1++ |
The Following 4 Users Say Thank You to Lars-Olof Leven For This Useful Post: | ||
Tom David (08-06-2009) |
Thread | Thread Starter | Forum | Replies | Last Post |
All time related values 0,0000001 time units delayed | Patrick Cap | Q&A | 2 | 05-01-2009 01:50 AM |
where to create operator TS ? | qin tian | Q&A | 3 | 10-22-2008 07:59 PM |
Can I make a column of a global table to type table? | qin tian | Q&A | 0 | 10-01-2008 09:27 PM |
More random random number | Paul Dowling | Q&A | 6 | 06-01-2008 07:30 PM |
Create tables on the fly | AlanZhang | Tips and Tricks | 0 | 08-24-2007 06:01 PM |