• 0

Random Number Generator


Question

24 answers to this question

Recommended Posts

  • 0

It'd help to know what language you need it in. Either I'm blind or you added that VB note as I posted this.

In C# I'd do:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> usedNumbers = new List<int>();
            string outv = "";
            Random r = new Random();

            for (int i = 0; i < 100; i++)
            {
                calc:
                int nextVal = r.Next(0, 101);
                if (usedNumbers.Contains(nextVal)) goto calc;
                outv += nextVal.ToString() + " ";
                usedNumbers.Add(nextVal);
            }
            outv = outv.Substring(0, outv.Length - 1);
            Console.Write(outv);
        }
    }
}

Here it is converted to VB:

Imports System.Collections.Generic
Imports System.Linq
Imports System.Text

Namespace Test
	Class Program
		Private Shared Sub Main(args As String())
			Dim usedNumbers As New List(Of Integer)()
			Dim outv As String = ""
			Dim r As New Random()

			For i As Integer = 0 To 99
				calc:
				Dim nextVal As Integer = r.[Next](0, 101)
				If usedNumbers.Contains(nextVal) Then
					GoTo calc
				End If
				outv += nextVal.ToString() & " "
				usedNumbers.Add(nextVal)
			Next
			outv = outv.Substring(0, outv.Length - 1)
			Console.Write(outv)
		End Sub
	End Class
End Namespace

  • 0
  On 03/10/2010 at 00:50, unr3al said:

I need to make a random number generator where it displays all the numbers 1-100 in a text box. All numbers need to be displayed once in a random order.

This has to be coded in visual basic. Can anyone help me out with the code?

Much appreciated if you do help!

It couldn't be easier :D Lucky for you VB already has a sort of RNG so really all you have to do is work on the presentation...

I found this doing a google on 'visual basic rng':

Private Function RandomNumber() as Integer

Randomize Timer

RandomNumber = Int(Rnd * 100)

End Sub

  • 0
  On 03/10/2010 at 01:00, omnicoder said:

It'd help to know what language you need it in. Either I'm blind or you added that VB note as I posted this.

In C# I'd do:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> usedNumbers = new List<int>();
            string outv = "";
            Random r = new Random();

            for (int i = 0; i < 100; i++)
            {
                calc:
                int nextVal = r.Next(0, 101);
                if (usedNumbers.Contains(nextVal)) goto calc;
                outv += nextVal.ToString() + " ";
                usedNumbers.Add(nextVal);
            }
            outv = outv.Substring(0, outv.Length - 1);
            Console.Write(outv);
        }
    }
}

Here it is converted to VB:

I'm so sorry :(

It has to be coded in VB. My fault, I updated my post.

I'd love if you could convert that to VB6.

  • 0

Private Shared Sub Main(args As String())
                        Dim usedNumbers As New List(Of Integer)()
                        Dim outv As String = ""

                        For i As Integer = 0 To 99
                                calc:
                                Dim nextVal As Integer = Rand(0, 101)
                                If usedNumbers.Contains(nextVal) Then
                                        GoTo calc
                                End If
                                outv += nextVal.ToString() & " "
                                usedNumbers.Add(nextVal)
                        Next
                        MsgBox(outv)
                End Sub

Can't test if that compiles as I no longer have VB6 but should be pretty close.

  • 0
  On 03/10/2010 at 01:06, omnicoder said:

Private Shared Sub Main(args As String())
                        Dim usedNumbers As New List(Of Integer)()
                        Dim outv As String = ""

                        For i As Integer = 0 To 99
                                calc:
                                Dim nextVal As Integer = Rand(0, 101)
                                If usedNumbers.Contains(nextVal) Then
                                        GoTo calc
                                End If
                                outv += nextVal.ToString() & " "
                                usedNumbers.Add(nextVal)
                        Next
                        MsgBox(outv)
                End Sub

Can't test if that compiles as I no longer have VB6 but should be pretty close.

I can't get it to compile. I'm a VB6 beginner, so I might be doing something wrong.

I appreciate your help though.

  • 0

Try

Private Sub Generate(args As String)
                        Dim usedNumbers As New List(Of Integer)()
                        Dim outv As String = ""

                        For i As Integer = 0 To 99
                                calc:
                                Dim nextVal As Integer = Rand(0, 101)
                                If usedNumbers.Contains(nextVal) Then
                                        GoTo calc
                                End If
                                outv = outv + nextVal.ToString() & " "
                                usedNumbers.Add(nextVal)
                        Next
                        MsgBox(outv)
                End Sub

  • 0
  On 03/10/2010 at 01:17, omnicoder said:

Try

Private Sub Generate(args As String)
                        Dim usedNumbers As New List(Of Integer)()
                        Dim outv As String = ""

                        For i As Integer = 0 To 99
                                calc:
                                Dim nextVal As Integer = Rand(0, 101)
                                If usedNumbers.Contains(nextVal) Then
                                        GoTo calc
                                End If
                                outv = outv + nextVal.ToString() & " "
                                usedNumbers.Add(nextVal)
                        Next
                        MsgBox(outv)
                End Sub

20896312.jpg

  • 0
  On 03/10/2010 at 01:22, omnicoder said:

Ah VB6 doesn't have a List class. I'll have to change it to an array. Gimmie a minute.

From the original post, and admittedly my recent programming skills are virtually non-existent as I learned to code pre-Windows, the above code won't do exactly what is requested.

As I understand the task,

"I need to make a random number generator where it displays all the numbers 1-100 in a text box. All numbers need to be displayed once in a random order."

Generating 100 random numbers between one and 100 won't guarantee there won't be repeats.

Instead, and I can't write the code for it, simply put the numbers 1-100 in an array, list, or whatever... Then you simply perform say, 100 shuffles of 1 number for another in the list, each shuffle randomly determined, then spit the results out. The exact number of shuffles is arbitary but you'd want enough to theoretically at least shuffle each number once. So shuffling each number in the list at least once would ensure about as random as you're gonna get.

  • 0
  On 03/10/2010 at 01:38, krasch said:

From the original post, and admittedly my recent programming skills are virtually non-existent as I learned to code pre-Windows, the above code won't do exactly what is requested.

As I understand the task,

"I need to make a random number generator where it displays all the numbers 1-100 in a text box. All numbers need to be displayed once in a random order."

Generating 100 random numbers between one and 100 won't guarantee there won't be repeats.

Instead, and I can't write the code for it, simply put the numbers 1-100 in an array, list, or whatever... Then you simply perform say, 100 shuffles of 1 number for another in the list, each shuffle randomly determined, then spit the results out. The exact number of shuffles is arbitary but you'd want enough to theoretically at least shuffle each number once. So shuffling each number in the list at least once would ensure about as random as you're gonna get.

omnicoder's code ensures each number will only be displayed once. It uses a calculation to determine a random number between 1 and 100. It stores displayed numbers in a list (an array, in the case of VB6) and checks the list before showing the next number, each time; if the number has been displayed, it will search for another number between 1 and 100.

His C# code works very nicely.

  • 0
  On 03/10/2010 at 01:41, Calum said:

omnicoder's code ensures each number will only be displayed once. It uses a calculation to determine a random number between 1 and 100. It stores displayed numbers in a list (an array, in the case of VB6) and checks the list before showing the next number, each time; if the number has been displayed, it will search for another number between 1 and 100.

His C# code works very nicely.

I need omnicoder's code to work in VB6!

Thanks for the replies guys.

  • 0
  On 03/10/2010 at 01:45, unr3al said:

I need omnicoder's code to work in VB6!

Thanks for the replies guys.

Out of curiosity, how much experience have you had with both programming and VB6? I'm just wondering, again out of curiosity, do you understand exactly what omnicoder's code does? If not, it may be beneficial if it is explained to you, so you learn from this :)

  • 0
  On 03/10/2010 at 01:50, Calum said:

Out of curiosity, how much experience have you had with both programming and VB6? I'm just wondering, again out of curiosity, do you understand exactly what omnicoder's code does? If not, it may be beneficial if it is explained to you, so you learn from this :)

I just started a couple of weeks ago.

I don't have any experience in C#; Visual Basic 6 is currently the only language I have experience with.

Explanations would help!

  • 0
  On 03/10/2010 at 01:53, unr3al said:

I just started a couple of weeks ago.

I don't have any experience in C#; Visual Basic 6 is currently the only language I have experience with.

Explanations would help!

I see. Once you and omnicoder have sorted out code which works and functions as it should, make sure you understand every line - every single part of it - and if not, ask questions here until you do :)

It really is important you understand what every line of this code does and why certain decisions were made. When I was first starting out learning programming, I was guilty of just copying code of those that helped me without ensuring I understood it; that only made it harder for me in the longrun (although I'm decent at it now and ensure I learn as much as possible).

  • 0
  On 03/10/2010 at 01:58, Calum said:

I see. Once you and omnicoder have sorted out code which works and functions as it should, make sure you understand every line - every single part of it - and if not, ask questions here until you do :)

It really is important you understand what every line of this code does and why certain decisions were made. When I was first starting out learning programming, I was guilty of just copying code of those that helped me without ensuring I understood it; that only made it harder for me in the longrun (although I'm decent at it now and ensure I learn as much as possible).

Thanks for the advice.

  • 0

This is a VBS script that will generate a radom list of 100 numbers.

It uses the Randomize method and Dictionary object, you should be

able to convert it to your VB 6 project.

Run this script using Cscript.exe or you will get a lot of messageboxes.

Save As RandomNumbers.vbs

Dim Dic :Set Dic = CreateObject("Scripting.Dictionary")
Dim a,i
 Randomize
    For i = 0 To 99
     a = Int((100 - i + 1) * Rnd + 1)
     If Not Dic.Exists(a) Then
      Dic.Add  a, a 
      WScript.Echo a
      WScript.Sleep 100
     End If
    Next

  • 0
  On 03/10/2010 at 16:54, jake1eye said:

This is a VBS script that will generate a radom list of 100 numbers.

It uses the Randomize method and Dictionary object, you should be

able to convert it to your VB 6 project.

Run this script using Cscript.exe or you will get a lot of messageboxes.

Save As RandomNumbers.vbs

Dim Dic :Set Dic = CreateObject("Scripting.Dictionary")
Dim a,i
 Randomize
    For i = 0 To 99
     a = Int((100 - i + 1) * Rnd + 1)
     If Not Dic.Exists(a) Then
      Dic.Add  a, a 
      WScript.Echo a
      WScript.Sleep 100
     End If
    Next

Could you please convert this to VB6?

  • 0

It would be a lot more efficient to generate an array of numbers from 1 to 100, and then shuffle it for a while. It'd result in O(n) complexity rather than O(n?), like the suggested solution.

Generating an array of 100 numbers from 1 to 100 is trivial.

As for shuffling, generate two random numbers between 0 and 99 (or 1 to 100 depending on whether VB6 indexes from 0 or 1, I'm not sure), and then swap the values located at these indexes in the array. Repeat at least 50 times. The more you repeat the more random it gets.

  • 0

i'm not trying to be an asshat here.. but these type of threads should be closed.

this is not helping you learn anything, and the type of problem you are solving is clearly a typical school/uni assignment..

i would have hoped people to have more sense than to just post up answer for you huh.gif

in any case, there is PLENTY enough detail in this thread to set you on your way to be able to do it in VB6.. you don't even have to work out how to solve the issue, but just write it on in another language..

good luck :)

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • The Nokia Lumias? Or the third-party HTC One8's? I had HTC's hardware cuz it was slick and reliable... but, yeah, the software left me wanting more and I just couldn't allocate personal time to develop all of the software I would have wanted to see (overworked in other capacities @ MSFT at the time, heh).
    • Microsoft's mobile strategy had great future vision and UX research, but mediocre engineering and inadequate support (third-party and internal business alike). The death knell for WinMo was Google's (mostly YouTube's) incessant API blocking and purposeful release of buggy WinMo builds to force consumers to stay away -- and this was conducted via sabotage of whatever partnerships they were supposed to play nice in. I still yearn for that UI on a modern smartphone...
    • Linux has always been an option but never adopted by the masses despite being free. The reasons are limited usability and features. Despite everything we all complaint about with MS , the overall experience for the general public is much better than what Linux can deliver.
    • If nothing works automatically for you, I'd say pick a better/different distro. Granted, it's trickier with laptops because they use all kinds of weird hardware, but still. I actually just did a fresh Arch Linux install on my netbook, and given that Arch is certainly not an "automagical" distro, I had to do very little manual tweaking, everything but the audio worked out of the box (including plasma and Wayland) and the audio was simply an issue of installing an additional firmware package that wasn't included in the default selection. Which is equivalent of installing additional drivers in Windows. Surely a more user-oriented distro would be even less troublesome (but granted, I haven't used/tested anything outside of Arch for quite some time). And let's not forget that a fair bit of issues that get blamed on Linux (though it also applies to Windows issues) are actually caused by hardware vendors not giving a damn.
    • The reason Linux will never succeed for consumers is simple. There are 800 distro’s and 400 active versions. As a consumer, “I want Linux” - great, which one? No single App Store, and websites will say “use this app, oh, sorry, doesn’t work on yours”. No single place for support, community help is reduced, so many apps don’t work or have compatibility problems. Drivers from manufacturers have issues. The gaming industry isn’t behind it. if you have an old pc, then sure, try Linux, you might love it. But the arguments above don’t stack up.
  • Recent Achievements

    • One Month Later
      POR2GAL4EVER earned a badge
      One Month Later
    • One Year In
      Orpheus13 earned a badge
      One Year In
    • One Month Later
      Orpheus13 earned a badge
      One Month Later
    • Week One Done
      Orpheus13 earned a badge
      Week One Done
    • Week One Done
      serfegyed earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      563
    2. 2
      ATLien_0
      256
    3. 3
      +Edouard
      163
    4. 4
      +FloatingFatMan
      157
    5. 5
      Michael Scrip
      109
  • Tell a friend

    Love Neowin? Tell a friend!