-1

Sorry this will probably sound really stupid and is probably answered somewhere but I can't find it and I'm seriously stuck, I'm trying to make a dice for a snakes and ladders game and I've got it to work ish except it displays the same pattern of numbers every time I reload the game. It goes 5,4,4,2,3,5,1.... exactly the same each time.

I had it like this:

Dim n As Integer = (1 + Rnd() * 5)

Then I tried changing it to this:

Dim n As Integer = CInt(Math.Floor((6 - 1 + 1) * Rnd())) + 1

But I still got the exact same numbers

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
Laura
  • 3
  • 2
  • 1
    [Randomize Function](https://msdn.microsoft.com/en-us/library/8zedbtdt(v=vs.90).aspx) They hide the answers to many problem on MSDN – Ňɏssa Pøngjǣrdenlarp Aug 31 '17 at 22:40
  • Check what Rnd() function outputs, does it have params? usually you need a range for a random generator. Second, following the basic rules of Algebra 6 - 1 + 1 is nonsense because you're adding and subtracting a constant (just leave it as (6)). – James Aug 31 '17 at 22:41
  • 1
    Don't use `Randomize` and `Rnd` at all. We're not in VB6 anymore Toto. Create a single instance of the `Random` class and call its `Next` method each time you need a random number. That's it, that's all. – jmcilhinney Sep 01 '17 at 03:04

2 Answers2

0

Randomize() initializes the first seed of Rnd(). If you won't use it - VB.NET will use the default seed number.

xmael91nt
  • 76
  • 3
0

Try using the Random class. It is in the System namespace.

dim random as Random = new Random();
dim randomNumber as int = random.Next();

There are overloads of the Next method so you can easily restrict the range of returned results.

Wayne Allen
  • 1,605
  • 1
  • 10
  • 16