Generate a random textfile from 1 to 500

Hi,

You can do this by doing the following:-

1) Create a List of Integers with the numbers from 1 to 500. The INDEX of the list for each number will then be from 0 to 499.

2) Use the Random class to then select an INDEX between 0 and the total number of items in the List, being 500

3) Use a StreamWriter to write the Random number to a file using the INDEX that was returned from the Random class.

4) Delete the current Number from the List that relates to the INDEX returned from the Random Class.

5) Put all this in a FOR loop and do it 500 times.

Hope that helps.

Cheers,

Ian
 
Similar approach, little bit simpler:

1) Create a list of integers from 1 to 500.
2) Shuffle that list.
3) Output the list to a file with StreamWriter.

Private Sub DoIt()
    Dim intList As New List(Of Integer)
    For i = 1 to 500
        intList.Add(i)
    Next

    intList = ShuffleList(intList)
    
    Dim objWriter As New StreamWriter("C:\SomeFile.txt")
    For Each Element As Integer In intList
        objWriter.Write(Element.ToString)
    Next
    objWriter.Close
   
End Sub

Private Function ShuffleList(ByVal lstInts As List(Of Integer)) As List(Of Integer)
    Dim Rnd As New Random()
    For idx As Integer = 0 To lstInts.Count - 1
        Dim idxRnd As Integer = Rnd.Next(idx, lstInts.Count - 1)
        Dim tmpInt As Integer = lstInts(idxRnd)
        lstInts(idxRnd) = lstInts(idx)
        lstInts(idx) = tmpInt
    Next
    ShuffleList = lstInts
End Function


Keep in mind this is notepad code...
 
Thanks!

I used Herman's code.
Because i a beginner, this is quite hard to understand.
Can i ask for one more help?

If i want this to extend a little bit, to 20 series of 1 to 500 mixed, all different mixed and at the end of each serie the text: Serie 1, Serie 2 etc,

Is that possible, or do i need a totally different code?
 
Hi,

By all means use Herman's example but please do try and understand the underlying concepts behind what Herman has posted using the links I have provided above. 3 comments for you:-

1) Have a look at the generated file. What is ALWAYS the last number in the file. Is that correct?

2) Did Herman make a small mistake (only a tinsy wincy one Herman)? If so, how do you fix this. Read the link on the Random Class.

3) To complete 20 iterations of each block of Random 500 numbers, you can do the following:-

a) Add another For Loop from 1 to 20
b) Rearrange Herman's code to Open the file, Shuffle and write the List 20 times and then finally Close the file.

Hope that helps.

Cheers.

Ian
 
Hello Ian,

You're right, it's important to undestand every bit of code.
I started learning VB.net last month, so everything is still very difficult for me.

I dont know how to random select a number from the list, and delete it after it has been written to the text file

This is what i do understand for 100%:

Imports System.IO
Module Module1
Sub Main()
Dim out1 As New StreamWriter("D:\file.txt")
Dim i As Integer
Dim Element As Integer
Dim List As New List(Of Integer)

For i = 1 To 500
List.Add(i)
Next

For Each Element In List
out1.WriteLine(Element.ToString("000"))
Next

out1.Close()

End Sub
End Module


 
Hi,

Ok, You have no Randomisation functionality in the code that you have posted so my questions are:-

1) Have you understood everything that Herman has posted? I guess not? If not, which parts are you struggling with?

2) Have you been introduced to Functions yet and do you know how they work?

3) Have you read the links that I posted?

4) I take it you are studying VB as one of your courses in your education?

Based on your answers we should be able to guide you further in what needs to be done to get this sorted.

Cheers,

Ian
 
Hi,

I have been practicing with some examples i found. I've got it working, i dont know of this is the best way to do it.
This is the code i have so far:

Imports System.IO
Module Module1
Sub Main()
Dim out1 As New StreamWriter("D:\file.txt")
Dim i As Integer
Dim rnd As New Random
Dim List As New List(Of Integer)

Dim index As Integer
Dim Item As Object
Dim MaxSeries As Integer = 20
Dim Serieindex As Integer

For Serieindex = 1 To MaxSeries
For i = 1 To 500
List.Add(i)
Next
While List.Count > 0
index = rnd.Next(0, List.Count)

Item = List(index)

List.RemoveAt(index)

out1.WriteLine(Item.ToString)
End While
out1.WriteLine("Serie: " + Serieindex.ToString)
Next
out1.Close()
End Sub
End Module


This gave the correct output.

But to answer you questions:
1. I did not understand everything Herman posted, the code in the Function is hard for me to understand
2. I heared a little about functions
3. Yes, i've read the links
4. No, im learning it in my free time, an some hours on my work (as today). I have a book, and use the internet to learn it. Do you know some good websites for learning VB.net

So far i did some tutorials, en created some thing myself, (things with textboxes, etc) those things, dont give me any problems so far. But those things i'm not going them often. For work i'll need things like: read out a textfile and create new files. But i cant find much lessons about it.
 
Hi,

I could suggest that your While loop should have been a For loop but other than that there is no reason for me to critique any of your code since that works great and you got the logic perfect, even finding Herman's slight error (sorry Herman).

Well done!

You could have a look at this tutorial to see if helps you further:-

Microsoft Visual Basic .NET tutorials for Beginners

Keep up the good work and hope that helps.

Cheers,

Ian

BTW, When posting code, please use the advanced button and add Code Tags to your code for readability.

Just noticed. One comment for you, when declaring variable names, do NOT use the same name as Existing classes or keywords in VB otherwise you will run into problems in the future. i.e., do NOT say:-

VB.NET:
Dim List As New List(Of Integer)

Say something else like:-

VB.NET:
Dim myListOfIntegers As New List(Of Integer)
 
Hi,

The basic difference between a FOR loop and a While loop is as follows:-

A FOR loop is used when you know the EXACT amount of times that you need to do something. i.e. If you needed to do something 500 times then you would say something like:-

VB.NET:
For Counter As Integer = 1 To 500
  'do the same code 500 times
Next

Whereas:-

A WHILE loop is used when you DO NOT know the number of times something should be done. As an example, if you Read a file, line by line, using a StreamReader, then you would typically not know how many lines need to be read from the file but you would need to read the file until the whole file had been read so you would say something like:-

VB.NET:
Dim myReader As New StreamReader("d:\myfile.txt")
While Not myReader.EndOfStream
  Dim myLine As String = myReader.ReadLine
End While

Hope that explains things.

Cheers,

Ian
 
What was my error exactly? Or are you referring to the fact that I declared my Random inside the routine? Not really an error, since that routine is the only place I use the Random. I could have declared it global if I needed to use it elsewhere...
 
Hi Herman,

The error was here:-

VB.NET:
Dim idxRnd As Integer = Rnd.Next(idx, lstInts.Count - 1)

In this statement your Random function never picks up the last number in the list since you this random statement picks a number between x and 498, hence missing index 499 in the list. Don't forget that Random.Next returns a random number Inclusive of the lower bound but Exclusive of the upper bound. Try it.

Hope that helps.

Cheers,

Ian
 
Back
Top