Click here to Skip to main content
14,268,557 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have this random number generator and I want to change it to VB.net.
I am getting confused on how to auto save and to program it.

What I have tried:

import random

number_of_codes = 100000
code_file = "codes.txt"

with open(code_file, "a+") as file:
    for _ in range (number_of_codes):
        a=random.randrange(10)
        b=random.randrange(10)
        c=random.randrange(10)
        d=random.randrange(10)
        e=random.randrange(10)

        file.write("282460000" + str(a) + str(b) + str(c) + str(d) + str(e))
        file.write("\n")

print ("Done!")
Posted
Updated 10-Aug-19 0:56am
v2
Rate this:
Please Sign up or sign in to vote.

Solution 2

Lets break this down into steps.
1. Open a file for writing
2. FOR loop
3. Random number generation
4. Writing to a file
5. Write a line to the console

References:
1. File.OpenWrite(String) Method (System.IO) | Microsoft Docs[^]
2. C# for statement | Microsoft Docs[^]
3. Random Class (System) | Microsoft Docs[^]
4. Included in #1
5. Console.Write Method (System) | Microsoft Docs[^]
   
Rate this:
Please Sign up or sign in to vote.

Solution 3

a=random.randrange(10)
b=random.randrange(10)
c=random.randrange(10)
d=random.randrange(10)
e=random.randrange(10)

file.write("282460000" + str(a) + str(b) + str(c) + str(d) + str(e))

Using 1 random function per digit is not a clever idea, it will not improve randomness of generated numbers. Random functions exist since beginning of computers and are perfectly capable of generating 5 digits numbers.
If your problem comes from leading zeros, there is solutions:
- when you convert number to string, you can use formatting (language dependent)
- or without formatting
a=random.randrange(100000)
b=str(100000+a)

then you cut first char of string, or keep the 5 chars on right of string.
   
Rate this:
Please Sign up or sign in to vote.

Solution 1

To be honest, I didn't understand what the problem you have, but if you need to create the random number in VB.NET you can do it like this:

[EDIT]
You forgot a line of code.
[/EDIT]

Dim r As New System.Random()
Dim value = r.Next(10)


HTH
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 4

Thanks a lot for all of you who helped me! Cheers!!
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100