Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
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

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[^]
 
Share this answer
 
Python
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
Python
a=random.randrange(100000)
b=str(100000+a)

then you cut first char of string, or keep the 5 chars on right of string.
 
Share this answer
 
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
 
Share this answer
 
v2
Thanks a lot for all of you who helped me! Cheers!!
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900