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 08-05-2009
Michael Hartlieb Michael Hartlieb is offline
Flexsim User
 
Join Date: Oct 2008
Location: Osnabrueck, Germany
Posts: 16
Downloads: 40
Uploads: 0
Thanks: 27
Thanked 2 Times in 1 Post
Rep Power: 143
Michael Hartlieb is on a distinguished road
Default 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  
Old 08-05-2009
Lars-Olof Leven Lars-Olof Leven is offline
Flexsim Distributor
 
Join Date: Aug 2007
Location: Sweden, Borlnge
Posts: 312
Downloads: 278
Uploads: 2
Thanks: 300
Thanked 256 Times in 139 Posts
Rep Power: 330
Lars-Olof Leven is a splendid one to beholdLars-Olof Leven is a splendid one to beholdLars-Olof Leven is a splendid one to beholdLars-Olof Leven is a splendid one to beholdLars-Olof Leven is a splendid one to beholdLars-Olof Leven is a splendid one to beholdLars-Olof Leven is a splendid one to behold
Default

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  
Old 08-05-2009
Michael Hartlieb Michael Hartlieb is offline
Flexsim User
 
Join Date: Oct 2008
Location: Osnabrueck, Germany
Posts: 16
Downloads: 40
Uploads: 0
Thanks: 27
Thanked 2 Times in 1 Post
Rep Power: 143
Michael Hartlieb is on a distinguished road
Default

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  
Old 08-05-2009
Lars-Olof Leven Lars-Olof Leven is offline
Flexsim Distributor
 
Join Date: Aug 2007
Location: Sweden, Borlnge
Posts: 312
Downloads: 278
Uploads: 2
Thanks: 300
Thanked 256 Times in 139 Posts
Rep Power: 330
Lars-Olof Leven is a splendid one to beholdLars-Olof Leven is a splendid one to beholdLars-Olof Leven is a splendid one to beholdLars-Olof Leven is a splendid one to beholdLars-Olof Leven is a splendid one to beholdLars-Olof Leven is a splendid one to beholdLars-Olof Leven is a splendid one to behold
Default

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;
    } 
}
Lars-Olof
The Following 2 Users Say Thank You to Lars-Olof Leven For This Useful Post:
Tom David (08-05-2009)
  #5  
Old 08-05-2009
Lars-Olof Leven Lars-Olof Leven is offline
Flexsim Distributor
 
Join Date: Aug 2007
Location: Sweden, Borlnge
Posts: 312
Downloads: 278
Uploads: 2
Thanks: 300
Thanked 256 Times in 139 Posts
Rep Power: 330
Lars-Olof Leven is a splendid one to beholdLars-Olof Leven is a splendid one to beholdLars-Olof Leven is a splendid one to beholdLars-Olof Leven is a splendid one to beholdLars-Olof Leven is a splendid one to beholdLars-Olof Leven is a splendid one to beholdLars-Olof Leven is a splendid one to behold
Default

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.
  1. Now I create a random number between 1 and the size of the table
  2. Read out the value in the table.
  3. Assign the value to an array
  4. Remove the row from the table.
  5. Now I start over with 1 until all my random numbers is generated.

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);
}
Need more information, let me know.

Lars-Olof
The Following User Says Thank You to Lars-Olof Leven For This Useful Post:
Brandon Peterson (08-05-2009)
  #6  
Old 08-05-2009
Michael Hartlieb Michael Hartlieb is offline
Flexsim User
 
Join Date: Oct 2008
Location: Osnabrueck, Germany
Posts: 16
Downloads: 40
Uploads: 0
Thanks: 27
Thanked 2 Times in 1 Post
Rep Power: 143
Michael Hartlieb is on a distinguished road
Default 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?
Attached Files
File Type: zip Double_Value.zip (31.0 KB, 199 views)
  #7  
Old 08-05-2009
Lars-Olof Leven Lars-Olof Leven is offline
Flexsim Distributor
 
Join Date: Aug 2007
Location: Sweden, Borlnge
Posts: 312
Downloads: 278
Uploads: 2
Thanks: 300
Thanked 256 Times in 139 Posts
Rep Power: 330
Lars-Olof Leven is a splendid one to beholdLars-Olof Leven is a splendid one to beholdLars-Olof Leven is a splendid one to beholdLars-Olof Leven is a splendid one to beholdLars-Olof Leven is a splendid one to beholdLars-Olof Leven is a splendid one to beholdLars-Olof Leven is a splendid one to behold
Default

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;
You need to add the code before the for-loop.

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)
  #8  
Old 08-05-2009
Michael Hartlieb Michael Hartlieb is offline
Flexsim User
 
Join Date: Oct 2008
Location: Osnabrueck, Germany
Posts: 16
Downloads: 40
Uploads: 0
Thanks: 27
Thanked 2 Times in 1 Post
Rep Power: 143
Michael Hartlieb is on a distinguished road
Default

Hi Lars-Olof,

many thanks, it works very good . That was, what I need


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


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.