Question Generate a 3 tab separated number file

SuperMario

Member
Joined
Mar 11, 2013
Messages
14
Location
The Netherlands, near Rotterdam
Programming Experience
Beginner
Let say i have four numbers . 1,2,3,4
I want to generate a number file that looks like this:

1 <tab> 1 <tab> 2
4 <tab> 4 <tab> 1
etc...
etc....

The important thing is, the first two numbers always need to be the same.

I want to create about 500 of these rows. Does not matter if some combinations are multiple in it.
whats the best way to do this?


Edit: i forgot the wordt "number" between 3 and tab in the title
 
Hi,

I am not sure what you are trying to do here, but here is an example of creating a string with Tabs using the numbers 1 to 4. How you manipulate this string is up to you:-

VB.NET:
Dim myRandomNumber As New Random
 
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
  Dim myNumbers() As Integer = {1, 2, 3, 4}
  Dim myString As String = myNumbers(0) & vbTab & myNumbers(0) & vbTab & myNumbers(myRandomNumber.Next(0, myNumbers.Length)) & vbTab & myNumbers(myRandomNumber.Next(0, myNumbers.Length))
 
  MsgBox(myString)
End Sub

All you need to do now is write this to a file.

Hope that helps.

Cheers,

Ian
 
Hi ian,

This come's close.

This writes 4 numbers tab separated, i only need 3.

I changed te code a little bit:

VB.NET:
[FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]  
[/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] myRandomNumber [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af]Random[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]
[FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af]
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]        
[/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] myNumbers() [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] = {1, 2, 3, 4}[/SIZE][/FONT][/SIZE][/FONT]
[FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]
        
[/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] myString [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]
[FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]
        
[/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] Index = 0 [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] 10[/SIZE][/FONT][/SIZE][/FONT]
[FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]
            myString = myNumbers(0) & vbTab & myNumbers(0) & vbTab & myNumbers(myRandomNumber.Next(0, myNumbers.Length))


            Out1.WriteLine(myString)

        
[/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]
[FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]
        Out1.Close()
[/SIZE][/FONT][/SIZE][/FONT]

The only thing is, the first two numbers are always 1, this need to change to. Both the first two need to be the same.

And one important thing is, all 3 numbers never may be the same..

I hope this is better to understand now?
 
Hi,

The only thing is, the first two numbers are always 1, this need to change to

I have already demonstrated how to get a random number from the list so use that idea to get the first number and then set the second number to the first number.

Both the first two need to be the same.

And one important thing is, all 3 numbers never may be the same..

It sounds to me like these two statements are in conflict of one another? If what you mean is that the third number cannot be the same as either the first or second number then you will need to create a While loop to generate a number, then check against the first or second number and, if not the same, then use it, otherwise keep looping until you get a number that you can use.

Hope that helps.

Cheers,

Ian
 
Hi Ian,

I created something yesterday. This works fine.

VB.NET:
Sub Main()
        Dim Index As Integer
        Dim Kaart As String
        Dim RndGetal As New Random(System.DateTime.Now.Millisecond)
        Dim Rnd As New Random
        Dim Item As Integer
        Dim Out1 As StreamWriter = New StreamWriter("test")
        Dim myNumbers1 As Integer
        Dim myNumbers2 As Integer
        Dim Numberlist As New List(Of Integer)

        For Index = 0 To 100
            myNumbers1 = RndGetal.Next(0, 5)
            myNumbers2 = RndGetal.Next(0, 5)
                If Not myNumbers1 = myNumbers2 Then
                Kaart = (myNumbers1.ToString + ControlChars.Tab + myNumbers1.ToString + ControlChars.Tab + myNumbers2.ToString)
                Out1.WriteLine(Kaart)
            End If
        Next
        Out1.Close()
    End Sub

to practice futher, i wanted to add an extra random number to it. this number can only be used once.
but i cant get it to work properly

VB.NET:
Sub Main()
        Dim Index As Integer
        Dim Kaart As String
        Dim RndGetal As New Random(System.DateTime.Now.Millisecond)
        Dim Rnd As New Random
        Dim Item As Integer

        Dim Out1 As StreamWriter = New StreamWriter("test")
        Dim myNumbers1 As Integer
        Dim myNumbers2 As Integer
        Dim Numberlist As New List(Of Integer)
        For Index = 0 To 100
            myNumbers1 = RndGetal.Next(0, 5)
            myNumbers2 = RndGetal.Next(0, 5)
            Numberlist.Add(Index)
            Index = Rnd.Next(0, Numberlist.Count)
            Item = Numberlist(Index)
            Numberlist.RemoveAt(Index)

            If Not myNumbers1 = myNumbers2 Then
                Kaart = (myNumbers1.ToString + ControlChars.Tab + myNumbers1.ToString + ControlChars.Tab + myNumbers2.ToString + ControlChars.Tab + Item.ToString("0000"))
                Out1.WriteLine(Kaart)
            End If
        Next
        Out1.Close()
    End Sub

I'm really stuck here..what am i doing wrong?
 
Hi,

I am sorry to say, but you have got this all wrong. Here are the main points for you:-

1) You use the variable Index to hold the current value assigned by the For Loop.

2) You then add the value of Index to a list.

3) You the set the value of the variable Index to a Random value between Zero and One which is always Zero. This is a BIG ERROR since you must NEVER interfere with a variable assigned by a For Loop otherwise the For Loop will never work as is the case here.

4) If you then got the above For loop sorted and continued to use the Index variable then would come across all sorts of "Index Out of Range" errors.

Here therefore is a simple example of how this can be done. I have deliberately NOT made the code efficient so as not to confuse you further but if you read the comments carefully you should be able to see, not only, how this works but why this works.

VB.NET:
Imports System.IO
 
Module Module1
 
  Sub Main()
    Dim myRandom As New Random
    Dim myRandomIndex As Integer
    Dim myOutputString As String
    Dim myNumbers1 As Integer
    Dim myNumbers2 As Integer
    Dim myNumbers3 As Integer
    Dim myNumbers4 As Integer
   
    'Encase your StreamWriter in a Using block so that the file is Disposed
    'correctly when the project ends and resources are returned to the system
    Using myWriter As New StreamWriter("c:\temp\testrandom.txt")
 
      'First we create a loop to do this 100 times
      For myLoopCounter As Integer = 1 To 100
        'Here we create a list of all the possible numbers that can be used in the project
        'By declaring this NumberList variable here, the list is effectively reset for each 
        'iteration of the Loop even though we remove items as part of the loop. 
        Dim Numberlist As New List(Of Integer) From {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
 
        'Firstly we get a random number from 0 to 9 which relates to an index in the NumberList variable
        myRandomIndex = myRandom.Next(0, Numberlist.Count)
        'We save the number at that particular index and set the second number to the same number
        myNumbers1 = Numberlist(myRandomIndex)
        myNumbers2 = myNumbers1
        'Now we remove the number in the list at the specified index so that we do not pick that number again
        Numberlist.RemoveAt(myRandomIndex)
 
        'Now we just do the same thing again for however many times you want. i.e:-
        'Get a Random Index
        'Save the Number at that Index
        'Delete the Number at that Index
        myRandomIndex = myRandom.Next(0, Numberlist.Count)
        myNumbers3 = Numberlist(myRandomIndex)
        Numberlist.RemoveAt(myRandomIndex)
 
        myRandomIndex = myRandom.Next(0, Numberlist.Count)
        myNumbers4 = Numberlist(myRandomIndex)
        Numberlist.RemoveAt(myRandomIndex)
 
        'Now we build the string and output to a file
        myOutputString = myNumbers1.ToString + ControlChars.Tab + myNumbers2.ToString + ControlChars.Tab + myNumbers3.ToString + ControlChars.Tab + myNumbers4.ToString
        myWriter.WriteLine(myOutputString)
      Next
    End Using
    MsgBox("Done!")
  End Sub
End Module

Hope that helps.

Cheers,

Ian
 
Hi Ian,

I understand it. but what you created is not what i was trying to do.

First a question, was this code Correct? :
VB.NET:
Sub Main()
        Dim Index As Integer
        Dim Kaart As String
        Dim RndGetal As New Random(System.DateTime.Now.Millisecond)
        Dim Rnd As New Random
        Dim Item As Integer
        Dim Out1 As StreamWriter = New StreamWriter("test")
        Dim myNumbers1 As Integer
        Dim myNumbers2 As Integer
        Dim Numberlist As New List(Of Integer)

        For Index = 0 To 100
            myNumbers1 = RndGetal.Next(0, 5)
            myNumbers2 = RndGetal.Next(0, 5)
                If Not myNumbers1 = myNumbers2 Then
                Kaart = (myNumbers1.ToString + ControlChars.Tab + myNumbers1.ToString + ControlChars.Tab + myNumbers2.ToString)
                Out1.WriteLine(Kaart)
            End If
        Next
        Out1.Close()
    End Sub


What i was trying to create was an random number between 1 and 100. This number can only be written once so it is an unique number.
So i thought is could use the index for it...but that was a bad idea!
 
Hi,

First a question, was this code Correct? :

Yes, if you are happy that you would get an unknown number of lines added to the file due to the If statement.

What i was trying to create was an random number between 1 and 100. This number can only be written once so it is an unique number.

You could expand the example I have already provided to say:-

VB.NET:
Dim Numberlist As List(Of Integer) = Enumerable.Range(1, 100).ToList

Then generate the Random number as shown previously.

So i thought is could use the index for it...but that was a bad idea!

Yes, Very Bad Idea. As I said you must NEVER change the value of a variable assigned by the For Loop.

Hope that helps.

Cheers,

Ian
 
A very simple approach is to use a set of numbers, and for each line reorder them randomly and take two numbers.
        Dim r As New Random
        Dim numbers = Enumerable.Range(1, 50) 'numbers 1-50
        For i = 1 To 100
            Dim picks = numbers.OrderBy(Function(x) r.Next).Take(2).ToArray
            Dim s = picks(0) & vbTab & picks(0) & vbTab & picks(1)

        Next
 
Hi,

Sorry, but what does this mean:-



Cheers,

Ian

Hello Ian,

I mean i want to insert 10 lines of the number combination 4 4 4 randomly between the rows we created in the example we gave you.
This was my first wish, to create rows like this:

2 2 1
3 3 4
1 1 2
etc... till we have 100 lines like this (first 2 numbers same, last different)

Now i'd like to add 10 lines of these:
4 4 4

Randomly mixed trough the file.

i hope you understand it..
 
Hi,

Ok, I think I have got my brain cells around what you are wanting to do, which is:-

1) You want 90 rows of your first combination being first two numbers the same and the last number different. i.e, 1,1,2 etc
2) Then you want an additional 10 rows of all three numbers the same. i.e, 4,4,4
3) Then you want these number rows Randomly written to a file.

So, to do that, lets do a combination of what both JohnH and I have shown you.

First of all you need two For loops. The first is from 1 to 90 to do what we have already done. The second is from 1 to 10 to get a Random number and then make all three numbers the same number.

Now, the issue here is that you MUST NOT write the results of the For loops directly to the file in the For loops since the final 10 rows of all the same numbers would ALWAYS come at the end of the file. Instead write the results of the For loops to a List of Integer Arrays. i.e:-

VB.NET:
'This is the defined List of Integer Arrays
Dim myForLoopResults As New List(Of Integer(2))
 
'To add your number array to the List use this definition
Dim myNumbers() As Integer = {myNumbers1, myNumbers2, myNumbers3}
myForLoopResults.Add(myNumbers)

Once this is done you now have a List of 100 Arrays generated by your For Loops so we can now use JohnH's example to Randomize that List and write the contents to a file. i.e:-

VB.NET:
Dim myRandomArrayList As List(Of Integer()) = myForLoopResults.OrderBy(Function(x) myRandom.NextDouble).ToList
 
For Each IntArray() As Integer In myRandomArrayList
  myOutputString = IntArray(0).ToString + ControlChars.Tab + IntArray(1).ToString + ControlChars.Tab + IntArray(2).ToString
  myWriter.WriteLine(myOutputString)
Next

That's it, Done!

Hope that helps.

Cheers,

Ian
 
Back
Top