Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to create an application with mvc arquitecture. I need to generate ramdom numbers 4 digits (not repeated) for users of my app. I mean, this method put on controller user , generate the numbers and show it in a view like a list (user + numbers asign). like a password

Do you have any idea? sorry fr my english
I need it for a test

What I have tried:

 public class User
    {

        [Key]
        public int UserId { get; set; }

       
        public int code { get; set; } // random number

       
        public string Name { get; set; }


        
        

        
    }
}




public class UserController : Controller
   {
       private DbContext db = new DbContext();

       // GET: Users
       public ActionResult Index()
       {
           var users = db.Users.Include(c => c.Lista);
           return View(users.ToList());
       }
Posted
Updated 15-Oct-17 20:54pm

Quote:
I need to generate ramdom numbers 4 digits (not repeated)

You have to make a choice!
Random and not repeated are mutually exclusive.
Random imply the possibility of repeat, and no repeat imply not random.
If you choose no repeat, think about the Shuffling algorithms
Shuffling - Wikipedia[^]
Other methods that keep track of previously picked number downgrade with quantity of picked numbers as it become more and more difficult to pick an unused number.
 
Share this answer
 
v3
Comments
NerakSeven 15-Oct-17 20:56pm    
thnks fr th explanation
The code which generates a random number between 1000 and 9999 is
C#
int randomNumber = Random.Next(1000, 10000);

Now, if you want to ensure uniqueness of this number, you have to keep track of previous generated values. Simplest way is to use a HashSet:
C#
HashSet<int> generatedNumbers = new HashSet<int>();
int randomNumber;

// Keep generating a random number until an unused value is found
do {
   randomNumber = Random.Next(1000, 10000);
} while (generatedNumbers.Contains(randomNumber));

generatedNumbers.Add(randomNumber);

// From here you have your random number that you can use
// This number has been added to the set so that it will not be chosen for next usages

Hope this helps. Kindly.
 
Share this answer
 
Comments
NerakSeven 15-Oct-17 21:04pm    
thnks a lot , I'll try it

I don't know the range of your random numbers, but if it is just a few thousand you could initialise a List and randomly index into the List removing each number chosen as you go. Something like this.


C#
Random random = new Random();
int upperBound = 1000;
List<int> Choices = Enumerable.Range(0, upperBound).ToList();
//Generate 1000 unique random numbers in the inclusive range 0-999
for(int i=0;i<upperBound;i++)
{
int index = random.Next(0, Choices.Count);
int choice = Choices[index];
Choices.RemoveAt(index);
}
 
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