Utoljára aktív 1762530029

easy way to generate a random int from to max

Revízió e31a9bf41d74149c403358c3465e27fb6d4d3b78

randomints.md Eredeti

If you need to generate a random integer, a number from say, 1 to 6, you can write a couple routines:

Java

import java.util.Random;

int min = 1;
int max = 6; 
Random random = new Random();

public int randomInt(min, max) {
  return random.nextInt((max + 1) - min + 1) + min;
}
// will return random ints from 1 to 6

Python

import random

def randomInt(min, max):
    return random.randint(min, max) 


# will return random ints from 1 to 6