ฦirฦirar Lambobin Random tsakanin 2 a cikin Harshen Shirye-shiryen C
ฦarfin ฦirฦira lambobin bazuwar na iya zama mahimmanci a wasu nau'ikan ayyuka na shirye-shiryen kwamfuta, musamman a ฦirar algorithm ko kuma inda ake buฦatar simulation. A cikin wannan labarin, za mu shiga cikin wani muhimmin al'amari na shirye-shiryen C, wanda ke samar da lambobi bazuwar. Za mu ษauka cewa kuna da ainihin fahimtar harshen shirye-shirye na C. C shine harshe na gaba ษaya mai ฦarfi wanda ke ba masu shirye-shirye ฦarin iko da inganci, kasancewa masu kyau ga shirye-shirye a ฦaramin matakin
Wataฦila kuna nan saboda kuna buฦatar bayani kan yadda ake samar da lambar bazuwar tsakanin 2 a cikin C. Wannan fasaha ce mai mahimmanci, musamman a cikin wasanni da kuma yanayin da kuke buฦatar kwaikwayi rashin tabbas a cikin shirye-shiryenku. Mu nutse a ciki.
Fahimtar Magani
A haฦiฦa, ra'ayin samar da lambar bazuwar tsakanin 2 a cikin C ya ฦunshi amfani da takamaiman ayyukan ษakin karatu da harshe ya samar da farko rand() da kuma srand() ayyuka. The rand() ana amfani da shi don samar da jerin lambobi na pseudorandom, amma don tabbatar da cewa waษannan lambobin ba su bi tsarin da ake iya ganewa ba, muna kuma amfani da su. srand() aiki, wanda ke shuka janareta na lambar bazuwar don ba mu mafi kyawun bazuwar.
Mahimmanci, ฦila za ka buฦaci tweak lambar bisa ga kewayon da kake son faษuwar lambobin bazuwar. Misali, idan kuna son lambar bazuwar tsakanin 1 da 2, zaku yi amfani da ma'auni daban daga lokacin da kuke son lamba tsakanin 2 da 100.
Bayanin mataki-mataki na Code
Anan ga cikakken toshe lambar don ฦirฦirar lamba tsakanin 2 ta amfani da yaren shirye-shiryen C:
#include <stdio.h> #include <stdlib.h> #include <time.h> int main() { srand(time(0)); int random_number = rand() % 2 + 1; printf("%dn", random_number); return 0; }
Bari mu karya lambar guda ษaya:
- Da farko mun haษa da madaidaicin madaidaicin ษakin karatu/fitarwa stdio.h da ma'auni na ษakin karatu stdlib.h cewa ya ฦunshi rand() da kuma srand() ayyuka.
- Muna amfani da janareta na bazuwar lamba lokaci (0). Wannan yana tabbatar da cewa kuna samun jerin lambobi daban-daban a duk lokacin da kuke gudanar da shirin ku.
- Bayan haka, muna samar da lambar bazuwar ta amfani da rand() aiki da aiki na modulus % 2 + 1 don taฦaita kewayon ฦirฦira lambobi daga 1 zuwa 2.
- A ฦarshe, muna amfani bugawa don buga lambar mu bazuwar zuwa console.
Zurfafa zurfafa: ษakunan karatu da ayyuka
A cikin wannan sashe, za mu nutse cikin manyan ayyuka guda biyu da muka yi amfani da su, rand() da kuma srand(), wanda ke aiki a ciki stdlib.h ษakin karatu. Waษannan ayyuka suna da kima yayin buฦatar lambobi bazuwar. Duk da haka, suna dogara ne da janareta na bazuwar da ke buฦatar shigarwar iri don tabbatar da cewa ba ta haifar da jeri ษaya na lambobi ba - lamarin da zai saษa wa manufarmu na cimma bazuwar. The srand() Ana amfani da aikin musamman a wannan batun.
A zahiri, amfani lokaci (0) kamar yadda shigarwar iri ke ba da tabbacin cewa duk lokacin da kuke gudanar da shirin, janareta na bazuwar bazuwar yana ษaukar iri daga wannan lokacin wanda ke ci gaba da canzawa don haka, jerin abubuwan fitarwa ya bambanta kowane lokaci.