programmieren_wise_24_25/Material/Untitled.ipynb
2024-11-21 17:56:00 +01:00

123 lines
3.5 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "d74e7711-ed1a-4749-8827-2e6fa5798d68",
"metadata": {},
"outputs": [],
"source": [
"def lcg (a,c,m, startwert):\n",
"\n",
" if a<=0 or c<0 or m<=0 or startwert <0:\n",
" return None #prüfung der werte \n",
" \n",
" x = startwert \n",
" while 1:\n",
" x=(a*x+c)%m\n",
" yield x "
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "2993ac89-2be8-4c61-a6e2-43a1008f2d36",
"metadata": {},
"outputs": [],
"source": [
"def lcg_test(seed: int, scalar: int, modulus: int, offset: int) -> int:\n",
" assert modulus > 0, \"Modulus must be greater than 0\"\n",
" assert 0 <= scalar and scalar < modulus, \"Scalar must be in range 0 <= a < m\"\n",
"\n",
" while seed > 1:\n",
" seed = (scalar*seed+offset) % modulus\n",
" assert seed >= 0\n",
" yield seed"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "02a21a6d-0892-44f0-b0fd-6e5f8fe83962",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Lcg using Cocktailshaker Numbers: 3089810780120156248\n",
"Correct should be: 3089810780120156248\n",
"\n",
"Lcg using Cocktailshaker Numbers: 8356396685252565260\n",
"Correct should be: 8356396685252565260\n",
"\n",
"Lcg using Cocktailshaker Numbers: 1921117399837525548\n",
"Correct should be: 1921117399837525548\n",
"\n",
"Lcg using Cocktailshaker Numbers: 14806858147081821235\n",
"Correct should be: 14806858147081821235\n",
"\n",
"Lcg using Cocktailshaker Numbers: 2557599628047639428\n",
"Correct should be: 2557599628047639428\n",
"\n",
"Lcg using Cocktailshaker Numbers: 16453652254840064460\n",
"Correct should be: 16453652254840064460\n",
"\n",
"Lcg using Cocktailshaker Numbers: 15995401842808378843\n",
"Correct should be: 15995401842808378843\n",
"\n",
"Lcg using Cocktailshaker Numbers: 681272290641816305\n",
"Correct should be: 681272290641816305\n",
"\n",
"Lcg using Cocktailshaker Numbers: 10955466795170118648\n",
"Correct should be: 10955466795170118648\n",
"\n",
"Lcg using Cocktailshaker Numbers: 13714992071537968180\n",
"Correct should be: 13714992071537968180\n",
"\n"
]
}
],
"source": [
"s = lcg(3203021881815356449, 11742185885288659963, 2**64-1, 3935559000370003845)\n",
"t = lcg_test(3935559000370003845, 3203021881815356449, 2**64-1, 11742185885288659963)\n",
"\n",
"for _ in range(10):\n",
" stud = next(s)\n",
" instructor = next(t)\n",
" print(\"Lcg using Cocktailshaker Numbers:\", stud)\n",
" print(\"Correct should be:\", instructor, end='\\n\\n')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "40aeb297-aeb5-4fca-8ae4-cb84c7f13957",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}