нужен генератор случайных чисел

Что-то, что здесь не упоминается и полезно: добавление суффикса к дню. Я отделил логику суффиксов, чтобы вы могли использовать ее для любого числа, которое вам нравится, а не только даты.

import time

def num_suffix(n):
    '''
    Returns the suffix for any given int
    '''
    suf = ('th','st', 'nd', 'rd')
    n = abs(n) # wise guy
    tens = int(str(n)[-2:])
    units = n % 10
    if tens > 10 and tens < 20:
        return suf[0] # teens with 'th'
    elif units <= 3:
        return suf[units]
    else:
        return suf[0] # 'th'

def day_suffix(t):
    '''
    Returns the suffix of the given struct_time day
    '''
    return num_suffix(t.tm_mday)

# Examples
print num_suffix(123)
print num_suffix(3431)
print num_suffix(1234)
print ''
print day_suffix(time.strptime("1 Dec 00", "%d %b %y"))
print day_suffix(time.strptime("2 Nov 01", "%d %b %y"))
print day_suffix(time.strptime("3 Oct 02", "%d %b %y"))
print day_suffix(time.strptime("4 Sep 03", "%d %b %y"))
print day_suffix(time.strptime("13 Nov 90", "%d %b %y"))
print day_suffix(time.strptime("14 Oct 10", "%d %b %y"))​​​​​​​
0
задан Ashkan Mobayen Khiabani 31 March 2019 в 05:54
поделиться

2 ответа

public class MyRandomGenerator
{
    private static readonly Random _randomGenerator = new Random();

    public static float NextFloat()
    {
        var randomValue = _randomGenerator.NextDouble() - 0.5d;

        return (float)randomValue;
    }
}
0
ответ дан Ernesto Andres Gutierrez 31 March 2019 в 05:54
поделиться

NextDouble генерирует случайные числа от 0 до 1.

private static Random random = new Random();
public static double GetRandomDouble_BetweenMinusHalfPlusHalf()
{
    return random.NextDouble() - 0.5;
}
0
ответ дан Theodor Zoulias 31 March 2019 в 05:54
поделиться
Другие вопросы по тегам:

Похожие вопросы: