public void OrdensChances_Compras()
        {
            Assert.AreEqual(100, OrdensChances.Compra(10, 10.02M));
            Assert.AreEqual(100, OrdensChances.Compra(10, 10.01M));
            Assert.AreEqual(100, OrdensChances.Compra(10, 10));
            Assert.AreEqual(99, OrdensChances.Compra(10, 9.99M));
            Assert.AreEqual(97, OrdensChances.Compra(10, 9.98M));
            Assert.AreEqual(94, OrdensChances.Compra(10, 9.97M));
            Assert.AreEqual(90, OrdensChances.Compra(10, 9.96M));
            Assert.AreEqual(85, OrdensChances.Compra(10, 9.95M));
            Assert.AreEqual(79, OrdensChances.Compra(10, 9.94M));
            Assert.AreEqual(72, OrdensChances.Compra(10, 9.93M));
            Assert.AreEqual(64, OrdensChances.Compra(10, 9.92M));
            Assert.AreEqual(55, OrdensChances.Compra(10, 9.91M));
            Assert.AreEqual(45, OrdensChances.Compra(10, 9.90M));
            Assert.AreEqual(34, OrdensChances.Compra(10, 9.89M));
            Assert.AreEqual(22, OrdensChances.Compra(10, 9.88M));
            Assert.AreEqual(9, OrdensChances.Compra(10, 9.87M));

            var random = OrdensChances.Compra(10, 9.86M);

            Assert.IsTrue(random > 0 && random < 1);
            Assert.IsTrue(random > 0 && random < 1);
            random = OrdensChances.Compra(10, 9.80M);
            Assert.IsTrue(random > 0 && random < 1);

            Assert.AreEqual(0, OrdensChances.Compra(10, 9.79M));
            Assert.AreEqual(0, OrdensChances.Compra(10, 9.78M));
            Assert.AreEqual(0, OrdensChances.Compra(10, 9.77M));
        }
        public async Task <ActionResult> Compra([Bind(Include = "OrdemId,Tipo,Preco,Quantidade,AcaoId")] Ordem ordem)
        {
            if (ModelState.IsValid)
            {
                var rd   = new Random((int)DateTime.Now.Ticks);
                var acao = await db.Acoes.FindAsync(ordem.AcaoId);

                ordem.OrdemId   = Guid.NewGuid();
                ordem.UsuarioId = Guid.Parse(User.Identity.GetUserId());
                ordem.DataHora  = DateTime.Now;
                ordem.Chance    = OrdensChances.Compra(acao.Preco, ordem.Preco);

                var percentual = (decimal)rd.NextDouble() * 100;
                if (ordem.Chance >= percentual)
                {
                    db.UsuariosAcoes.Add(new UsuarioAcao()
                    {
                        UsuarioAcaoId       = Guid.NewGuid(),
                        AcaoId              = ordem.AcaoId,
                        Ativo               = true,
                        Compra              = DateTime.Now,
                        Preco               = ordem.Preco,
                        UsuarioId           = ordem.UsuarioId,
                        Quantidade          = ordem.Quantidade,
                        Total               = ordem.Preco * ordem.Quantidade,
                        OrdensUsuariosAcoes = new List <OrdemUsuarioAcao>()
                        {
                            new OrdemUsuarioAcao
                            {
                                OrdemUsuarioAcaoId = Guid.NewGuid(),
                                OrdemId            = ordem.OrdemId
                            }
                        }
                    });
                }

                db.Ordens.Add(ordem);
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            await AcoesViewBag();

            return(View(ordem));
        }