Randomizing students in class

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

42 Replies - 2276 Views - Last Post: 31 October 2013 - 11:55 AM Rate Topic: -----

#31 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,500
  • Joined: 29-May 08

Re: Randomizing students in class

Posted 12 October 2013 - 09:13 PM

Wrong C1(4)='e' all collections in .net are Zero indexed based. First item is at index position 0.

Let me just rename the variable and remove few things to focus on the core aspects of the problem
For LoopIterationNumber = 0 To 7
  Dim PickedIndex = GenerateARandomNumberForMe.Next(0, ItemsToPickFrom.Count) ' This'll be a whole number (Integer)
  ' PickedIndex contains a random index position. It's just a number.
  '  ItemsToPickFrom.Count going to change each iteration of the loop
  ' because of the following line
  ItemsToPickFrom.RemoveAt( PickedIndex )
  ' where we remove the item at the index position.
  '
  ' Q:- Where is the item we removed? Or the Item (or Value) we wanted to be picked?
  '
  PickedItem.Add( PickedIndex ) ' You add the PickedIndex not the PickedItem
  ' Think the range of items to choose from is get smaller, thus the index range is getting smaller.
  ' So this doesn't match the full index of the original 16, but the random index of an item
  ' in a collection that is getting smaller each iteration

  Console.WriteLine( PickedItems.Item( LoopIterationNumber ) )
  ' This is going output on the first 8 items from PickedItems, everytime this block of code is called.
Next



What do we (you) need to do the code above to add the PickedItem to the List of PickedItems?

Hint: Value <> Index "E" <> 4
Was This Post Helpful? 1
  • +
  • -

#32 Raminator  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 283
  • Joined: 16-July 12

Re: Randomizing students in class

Posted 18 October 2013 - 11:19 AM

I think we need something like this:
PickedItem.Item(PickedIndex) = PickedIndex+1 'Because students count from 1 and the index starts from 0

Is this right?
Was This Post Helpful? 0
  • +
  • -

#33 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,500
  • Joined: 29-May 08

Re: Randomizing students in class

Posted 18 October 2013 - 06:33 PM

Nope. Imagine instead of numeric values for the ID they where strings.
Was This Post Helpful? 0
  • +
  • -

#34 Raminator  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 283
  • Joined: 16-July 12

Re: Randomizing students in class

Posted 18 October 2013 - 08:33 PM

We need to store the string to the index we have created in
PickedItem.Add( PickedIndex )
. Because here we remove ir from the items to pick from, then add an index to picked item, without transfering that value we picked to the picked index.
Was This Post Helpful? 0
  • +
  • -

#35 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,500
  • Joined: 29-May 08

Re: Randomizing students in class

Posted 19 October 2013 - 05:28 PM

Why are the still blinded by the index.

We need remember the Value at he pickedIndex of the Items to pick from.
Add that value to the picked items. Then remove the item at the index.
Dim pickedIndex = rng.Next( ItemsToPickFrom.Count )
Dim pickedValue = ItemsToPickFrom( pickedIndex )
PickedItems.Add( PickedValue )
ItemsToPickFrom.RemoveAt( pickedIndex )



We have to do this because .RemoveAt(index) doesn't return the Value at the index position. If it did return the value as well as removing it from the list I could write.
PickedItems.Add( ItemsToPickFrom.RemoveAt( rng.Next( ItemsToPickFrom.Count )))


Was This Post Helpful? 1
  • +
  • -

#36 Raminator  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 283
  • Joined: 16-July 12

Re: Randomizing students in class

Posted 20 October 2013 - 07:07 PM

I get it!! I really get it!! My confusion was the similarities between the index value and the student value, i was dealing with indexes thinking i was dealing with the actual students! How silly of me!
I'm looking into the working code you posted, a really short working version of mine from a few posts ago, and i wanted to know, what is a an iterator function?
Was This Post Helpful? 0
  • +
  • -

#37 Raminator  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 283
  • Joined: 16-July 12

Re: Randomizing students in class

Posted 22 October 2013 - 06:12 PM

Hey,AdamSpeight2008, would there be any problems for you if i moved on my project using that piece of code of yours? It ended up a lot smaller than mine. If you allow me too the code will be looking like this:
Option Strict On
Imports System.Windows.Forms.PaintEventArgs
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms.Control
Public Class Main
    Dim CIMG As Image = My.Resources.Grid_blank
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        For Yloop As Int32 = 1 To 3 Step 1
            For Cloop As Int32 = 1 To 5 Step 1
                TC1.Items.Add(String.Format("{0}00{1}", Yloop, Cloop))
                TC2.Items.Add(String.Format("{0}00{1}", Yloop, Cloop))
            Next
        Next
        TC1.SelectedItem = "1001"
        TC2.SelectedItem = "2001"
        For Anloop As Int32 = 1 To 32 Step 1
            AC1S.Items.Add(Anloop)
            AC1E.Items.Add(Anloop)
            AC2S.Items.Add(Anloop)
            AC2E.Items.Add(Anloop)
        Next
        AC1S.SelectedItem = 1
        AC1E.SelectedItem = 16
        AC2S.SelectedItem = 1
        AC2E.SelectedItem = 16
    End Sub
    Private Sub Go_Click(sender As Object, e As EventArgs) Handles Go.Click
        CIMG = My.Resources.Grid_blank
        Dim Psize As New Size(200, 401)
        Dim Rowval() As Int32 = {2, 103, 203, 303}
        Dim Line() As Int32 = {4, 102, 202, 302, 402, 503, 603, 703}
        Dim C1 = Enumerable.Range(CInt(AC1S.SelectedItem), CInt(AC1E.SelectedItem)).ToList
        Dim C2 = Enumerable.Range(CInt(AC2S.SelectedItem), CInt(AC2E.SelectedItem)).ToList
        Dim Rows As New List(Of List(Of String))
        For r = 1 To 4
            Dim f = (r Mod 2) = 1
            Rows.Add(Randorganize(8, If(f, C1, C2), CStr(If(f, TC1.SelectedItem, TC2.SelectedItem))).ToList)
        Next
        For rn = 0 To 3
            For sn = 0 To 7
                CIMG = mountimg(CIMG, Rowval(rn), Line(sn), Rows(rn)(sn), 12)
            Next
        Next
        Grid.Image = ResizeImage(CIMG, Psize, True)
        Rows.Clear()
    End Sub
    Private ran As New Random
    Iterator Function Randorganize(Linen As Integer, STN As List(Of Integer), CTN As String) As IEnumerable(Of String)
        For i = 1 To Linen
            Dim rs = ran.Next(STN.Count) ' This is picking a random number, not a random student.
            Dim sn = STN(rs) ' <-- This is the random student
            Yield String.Format("Aluno N:{0,2}{1}Turma:{2}", sn, Environment.NewLine, CTN)
            STN.RemoveAt(rs)
        Next
    End Function
    Function mountimg(ByVal inmage As Image, ByVal row As Int32, ByVal line As Int32, ByVal txt As String, ByVal Fsize As Int32) As Image
        Dim pimg As Image = inmage
        Dim img As System.Drawing.Graphics
        img = Graphics.FromImage(pimg)
        img.SmoothingMode = SmoothingMode.HighQuality
        img.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit
        img.CompositingQuality = CompositingQuality.HighQuality
        Dim drawstr As [String] = txt
        Dim font As New Font("Times New Roman", Fsize)
        Dim brush As New SolidBrush(Color.Black)
        Dim point As New PointF(row, line)
        img.DrawString(drawstr, font, brush, point)
        Return (pimg)
        img.Dispose()
        pimg.Dispose()
    End Function
    Private Sub Augment_Click(sender As Object, e As EventArgs) Handles Augment.Click
        Dim picform As New Form
        picform.Size = New Size(CIMG.Width + 50, CIMG.Height + 50)
        '      picform.AutoScrollMargin = New Size(50, 50)
        picform.AutoScroll = True
        Dim bpic As New PictureBox()
        bpic.Size = CIMG.Size
        picform.Controls.Add(bpic)
        bpic.Image = CIMG
        picform.Show()
    End Sub
    Public Shared Function ResizeImage(ByVal image As Image, _
      ByVal size As Size, Optional ByVal preserveAspectRatio As Boolean = True) As Image
        Dim newWidth As Int32
        Dim newHeight As Int32
        If preserveAspectRatio Then
            Dim originalWidth As Int32 = image.Width
            Dim originalHeight As Int32 = image.Height
            Dim percentWidth As Single = CSng(size.Width) / CSng(originalWidth)
            Dim percentHeight As Single = CSng(size.Height) / CSng(originalHeight)
            Dim percent As Single = If(percentHeight < percentWidth,
                    percentHeight, percentWidth)
            newWidth = CInt(originalWidth * percent)
            newHeight = CInt(originalHeight * percent)
        Else
            newWidth = size.Width
            newHeight = size.Height
        End If
        Dim newImage As Image = New Bitmap(newWidth, newHeight)
        Using graphicsHandle As Graphics = System.Drawing.Graphics.FromImage(newImage)
            graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic
            graphicsHandle.DrawImage(image, 0, 0, newWidth, newHeight)
        End Using
        Return newImage
        newImage.Dispose()
        image.Dispose()
    End Function
    Private Sub Save_Click(sender As Object, e As EventArgs) Handles Save.Click
        Savediag.Filter = "PNG Image|*.png"
        Savediag.FileName = String.Format("{0}\Turma.png", System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments))
        Savediag.ShowDialog()
        CIMG.Save(Savediag.FileName)
    End Sub
    Private Sub Print_Click(sender As Object, e As EventArgs) Handles Print.Click
        Printdiag.ShowDialog()
    End Sub
End Class


My question now is something i have been trying to do for a while now, print the image. I got the printing dialog to work, and i understood i need to have a print document for it, but my strugle is how to put my image, stored in CIMG, on tha print document. How can i do this? Is there an specific way of doing this?
Was This Post Helpful? 0
  • +
  • -

#38 Raminator  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 283
  • Joined: 16-July 12

Re: Randomizing students in class

Posted 24 October 2013 - 12:45 PM

Ok, so, two things for today.
First, i discovered a weird bug.When you change the student range, lets say on class 1 instead of from 1 to 16 i use from 17 to 32, the randomizing outputs are completely weird, i get outputs that range from 17 up to 46, i have no idea why this happens, i'll look further into it.
Second, i made some development on the print function but nothing great, currently reading this and this about printing with vb.net.
Code (small changes since last post):
Option Strict On
Imports System.Windows.Forms.PaintEventArgs
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms.Control
Public Class Main
    Dim CIMG As Image = My.Resources.Grid_blank
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        For Yloop As Int32 = 1 To 3 Step 1
            For Cloop As Int32 = 1 To 5 Step 1
                TC1.Items.Add(String.Format("{0}00{1}", Yloop, Cloop))
                TC2.Items.Add(String.Format("{0}00{1}", Yloop, Cloop))
            Next
        Next
        TC1.SelectedItem = "1001"
        TC2.SelectedItem = "2001"
        For Anloop As Int32 = 1 To 32 Step 1
            AC1S.Items.Add(Anloop)
            AC1E.Items.Add(Anloop)
            AC2S.Items.Add(Anloop)
            AC2E.Items.Add(Anloop)
        Next
        AC1S.SelectedItem = 1
        AC1E.SelectedItem = 16
        AC2S.SelectedItem = 1
        AC2E.SelectedItem = 16
    End Sub
    Private Sub Go_Click(sender As Object, e As EventArgs) Handles Go.Click
        CIMG = My.Resources.Grid_blank
        Dim Psize As New Size(200, 401)
        Dim Rowval() As Int32 = {2, 103, 203, 303}
        Dim Line() As Int32 = {4, 102, 202, 302, 402, 503, 603, 703}
        Dim C1 = Enumerable.Range(CInt(AC1S.SelectedItem), CInt(AC1E.SelectedItem)).ToList
        Dim C2 = Enumerable.Range(CInt(AC2S.SelectedItem), CInt(AC2E.SelectedItem)).ToList
        Dim Rows As New List(Of List(Of String))
        For r = 1 To 4
            Dim f = (r Mod 2) = 1
            Rows.Add(Randorganize(8, If(f, C1, C2), CStr(If(f, TC1.SelectedItem, TC2.SelectedItem))).ToList)
        Next
        For rn = 0 To 3
            For sn = 0 To 7
                CIMG = mountimg(CIMG, Rowval(rn), Line(sn), Rows(rn)(sn), 12)
            Next
        Next
        Grid.Image = ResizeImage(CIMG, Psize, True)
        Rows.Clear()
    End Sub
    Private ran As New Random
    Iterator Function Randorganize(Linen As Integer, STN As List(Of Integer), CTN As String) As IEnumerable(Of String)
        For i = 1 To Linen
            Dim rs = ran.Next(STN.Count) ' This is picking a random number, not a random student.
            Dim sn = STN(rs) ' <-- This is the random student
            Yield String.Format("Aluno N:{0,2}{1}Turma:{2}", sn, Environment.NewLine, CTN)
            STN.RemoveAt(rs)
        Next
    End Function
    Function mountimg(ByVal inmage As Image, ByVal row As Int32, ByVal line As Int32, ByVal txt As String, ByVal Fsize As Int32) As Image
        Dim pimg As Image = inmage
        Dim img As System.Drawing.Graphics
        img = Graphics.FromImage(pimg)
        img.SmoothingMode = SmoothingMode.HighQuality
        img.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit
        img.CompositingQuality = CompositingQuality.HighQuality
        Dim drawstr As [String] = txt
        Dim font As New Font("Times New Roman", Fsize)
        Dim brush As New SolidBrush(Color.Black)
        Dim point As New PointF(row, line)
        img.DrawString(drawstr, font, brush, point)
        Return (pimg)
        img.Dispose()
        pimg.Dispose()
    End Function
    Private Sub Augment_Click(sender As Object, e As EventArgs) Handles Augment.Click
        Dim picform As New Form
        picform.Size = New Size(CIMG.Width + 50, CIMG.Height + 50)
        '      picform.AutoScrollMargin = New Size(50, 50)
        picform.AutoScroll = True
        Dim bpic As New PictureBox()
        bpic.Size = CIMG.Size
        picform.Controls.Add(bpic)
        bpic.Image = CIMG
        picform.Show()
    End Sub
    Public Shared Function ResizeImage(ByVal image As Image, _
      ByVal size As Size, Optional ByVal preserveAspectRatio As Boolean = True) As Image
        Dim newWidth As Int32
        Dim newHeight As Int32
        If preserveAspectRatio Then
            Dim originalWidth As Int32 = image.Width
            Dim originalHeight As Int32 = image.Height
            Dim percentWidth As Single = CSng(size.Width) / CSng(originalWidth)
            Dim percentHeight As Single = CSng(size.Height) / CSng(originalHeight)
            Dim percent As Single = If(percentHeight < percentWidth,
                    percentHeight, percentWidth)
            newWidth = CInt(originalWidth * percent)
            newHeight = CInt(originalHeight * percent)
        Else
            newWidth = size.Width
            newHeight = size.Height
        End If
        Dim newImage As Image = New Bitmap(newWidth, newHeight)
        Using graphicsHandle As Graphics = System.Drawing.Graphics.FromImage(newImage)
            graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic
            graphicsHandle.DrawImage(image, 0, 0, newWidth, newHeight)
        End Using
        Return newImage
        newImage.Dispose()
        image.Dispose()
    End Function
    Private Sub Save_Click(sender As Object, e As EventArgs) Handles Save.Click
        Savediag.Filter = "PNG Image|*.png"
        Savediag.FileName = String.Format("{0}\Turma.png", System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments))
        Savediag.ShowDialog()
        CIMG.Save(Savediag.FileName)
    End Sub
    Private Sub Print_Click(sender As Object, e As EventArgs) Handles Print.Click
        Printdiag.ShowDialog()
    End Sub
End Class


Was This Post Helpful? 0
  • +
  • -

#39 Raminator  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 283
  • Joined: 16-July 12

Re: Randomizing students in class

Posted 24 October 2013 - 04:45 PM

Got the printing done, I'll now focus on the student range bug, any help would be very welcome.
Printing code:
  
Option Strict On
Imports System.Windows.Forms.PaintEventArgs
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms.Control
Imports System.Drawing.Printing
Imports System.IO
Public Class Main
    Dim CIMG As Image = My.Resources.Grid_blank
    Private ran As New Random
    Dim memstream As MemoryStream
    Dim WithEvents PI As New Printing.PrintDocument
  Private Sub Gprint(sender As Object, e As PrintPageEventArgs) Handles PI.PrintPage
        memstream = New MemoryStream
        CIMG.Save(memstream, Imaging.ImageFormat.Png)
        e.Graphics.DrawImage(Image.FromStream(memstream), e.Graphics.VisibleClipBounds)
        e.HasMorePages = False
        memstream.Close()
    End Sub
    Private Sub Print_Click(sender As Object, e As EventArgs) Handles Print.Click
        Printdiag.Document = PI
        Printdiag.ShowDialog()
        PI.Print()
    End Sub
End Class  

This post has been edited by Raminator: 24 October 2013 - 04:47 PM

Was This Post Helpful? 0
  • +
  • -

#40 Raminator  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 283
  • Joined: 16-July 12

Re: Randomizing students in class

Posted 24 October 2013 - 05:22 PM

Fixed range bug by switching
        Dim C1 = Enumerable.Range(CInt(AC1S.SelectedItem), CInt(AC1E.SelectedItem)).ToList
        Dim C2 = Enumerable.Range(CInt(AC2S.SelectedItem), CInt(AC2E.SelectedItem)).ToList

for the original
        For lc1 As Int32 = CInt(AC1S.SelectedItem) To CInt(AC1E.SelectedItem) Step 1
            C1.Add(lc1)
        Next
        For lc2 As Int32 = CInt(AC2S.SelectedItem) To CInt(AC2E.SelectedItem) Step 1
            C2.Add(lc2)
        Next

Was This Post Helpful? 0
  • +
  • -

#41 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,500
  • Joined: 29-May 08

Re: Randomizing students in class

Posted 25 October 2013 - 07:16 PM

View PostRaminator, on 21 October 2013 - 03:07 AM, said:

... i wanted to know, what is a an iterator function?


Iterator Tutorial

Also

What's with the cryptic identifiers? They mean absolutely nothing to me.
You realise the any code after a return doesn't execute.
  Return newImage    ' Exits the function 
  ' Any code after it is not executed.
  newImage.Dispose()
  image.Dispose()
End Function


This post has been edited by AdamSpeight2008: 25 October 2013 - 07:22 PM

Was This Post Helpful? 1
  • +
  • -

#42 Raminator  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 283
  • Joined: 16-July 12

Re: Randomizing students in class

Posted 27 October 2013 - 05:36 PM

Oops, those were my bad, i had keyboard problem that made impossible to press the ' key to comment those out, so i just put them there and forgot about it, thanks for seeing that.
Also, greate tutorial, thanks a lot for writing that, as iterators are fairly new in vb they lack quality documentation.
Was This Post Helpful? 0
  • +
  • -

#43 Raminator  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 283
  • Joined: 16-July 12

Re: Randomizing students in class

Posted 31 October 2013 - 11:55 AM

So, this project here is pretty much done, just some polishing needed, I'll be doing this for the next few days.
I'd like to thank lar3ry a lot for the help and also AdamSpeight2008 for having the patience to teach me and show me my mistakes on index and values, also thanks a lot for writing the tutorial for iterators. I'll post a final code when I'm finished.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3