dimanche 24 janvier 2016

Random divisable at least one of two integers in c#

I've got an integer that I'd like to assign a value to by using Random. The randomly chosen number must be divisable by 2 or 5 (or both). For divisable only by two I would just multiply the Random result * 2, but it's not an option here.

How to do this?

edit: I came up with this code, it's probably very inefficient though:

static void Main(string[] args)
        {
            int[] tab = new int[5];
            Random random = new Random();
            int i = 0;
            while (i < tab.Length)
            {
                int tempInteger = random.Next(101);
                if (tempInteger % 2 == 0 || tempInteger % 5 == 0)
                {
                    tab[i] = tempInteger;
                    i++;
                }
            }
        }




Aucun commentaire:

Enregistrer un commentaire