An warware: lambar bazuwar tsakanin 2 a C

ฦ˜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.

Shafi posts:

Leave a Comment