Machineboy空
C# 난수 생성 , Random.Next(), Random.NextDouble() 본문
Random Seed란?
컴퓨터 프로그램에서 발생하는 무작위 수는 사실 엄격한 의미의 무작위 숫가 아니다.
특정한 시작 숫자(seed)를 정해주면 컴퓨터가 정해진 알고리즘에 의해 마치 난수처럼 보이는 수열을 생성한다.
seed 값이 고정될 경우, 생성되는 랜덤 값이 동일해진다.
https://machineboy0.tistory.com/20
예제 코드
using System;
public class Player
{
// 난수 생성기 생성
private Random random = new Random();
// Int 형 난수 : 1 ~ 18
public int RollDie()
{
// 파라미터 a이상 b 미만
return random.Next(1,19);
}
// Double형 난수
public double GenerateSpellStrength()
{
// 파라미터는 따로 들어가지 않고, NextDouble()은 0.0이상 1.0 미만의 실수를 생성한다.
return random.NextDouble()*100;
}
}
static 클래스 선언, BigInteger 다루는 법 등.
위의 문법은 틀렸고 아래 문법은 맞음.
using System;
using System.Numerics;
public static class DiffieHellman
{
// STEP 0. 소수 p, g
// STEP 1. 각자의 개인키 a, b // 1 < a < p, a != b
// STEP 2. 공개키 A,B 계산식
// STEP 3. 서로의 공개키를 가지고 secret key s를 계산, 같은 결과를 가진다.
private static Random random = new Random();
public static BigInteger PrivateKey(BigInteger primeP)
{
return new BigInteger(random.Next(2, (int)primeP));
}
public static BigInteger PublicKey(BigInteger primeP, BigInteger primeG, BigInteger privateKey)
{
BigInteger c = BigInteger.ModPow(primeG, privateKey, primeP);
return c;
}
public static BigInteger Secret(BigInteger primeP, BigInteger publicKey, BigInteger privateKey)
{
BigInteger c = BigInteger.ModPow(publicKey, privateKey, primeP);
return c;
}
}
using System;
using System.Numerics;
public static class DiffieHellman
{
// STEP 0. 소수 p, g
// STEP 1. 각자의 개인키 a, b // 1 < a < p, a != b
// STEP 2. 공개키 A,B 계산식
// STEP 3. 서로의 공개키를 가지고 secret key s를 계산, 같은 결과를 가진다.
private static Random random = new Random();
public static BigInteger PrivateKey(BigInteger primeP)
{
return new BigInteger(random.Next(2, (int)primeP));
}
public static BigInteger PublicKey(BigInteger primeP, BigInteger primeG, BigInteger privateKey)
{
BigInteger c = BigInteger.ModPow(primeG, privateKey, primeP);
return c;
}
public static BigInteger Secret(BigInteger primeP, BigInteger publicKey, BigInteger privateKey)
{
BigInteger c = BigInteger.ModPow(publicKey, privateKey, primeP);
return c;
}
}