0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

乱数生成コードの比較(C言語 vs. C# vs. Python vs. JavaScript)

Posted at

頭が混乱してきたので、まとめることに。
※コードは、ChatGPTで生成。

C言語

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
    srand((unsigned int)time(NULL)); // 乱数の種を現在時刻で初期化
    int randomNumber = rand() % 100; // 0~99の乱数を生成
    printf("Random Number: %d\n", randomNumber);
    return 0;
}

C#

using System;

class Program {
    static void Main() {
        Random random = new Random();
        int randomNumber = random.Next(0, 100); // 0~99の乱数を生成
        Console.WriteLine($"Random Number: {randomNumber}");
    }
}

Python

import random

random_number = random.randint(0, 99) # 0~99の乱数を生成
print(f"Random Number: {random_number}")

JavaScript

  1. Math.random()は、0.0以上~1.0未満の値を生成
  2. これを100倍して、0.0以上~100.0未満の値に
  3. Math.floor()で、端数を切り捨てて整数に
const randomNumber = Math.floor(Math.random() * 100); // 0~99の乱数を生成
console.log(`Random Number: ${randomNumber}`);
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?