(Bash): Umurnin don samar da bazuwar lamba

Wani lokaci, muna shirya wasu rubutun a ciki Bash …. kuma muna buƙatar (saboda wasu dalilai) don samar da wasu lambobin bazuwar.

Don haka zaku iya shirya dukkan aikace-aikace (ko aiki ...) ee, amma ... mai ban mamaki tsarinmu ya riga ya aikata hakan 😀

A cikin m, rubuta mai zuwa kuma latsa [Shiga]:

amsa kuwwa $ RANDOM

... lamba zata bayyana, sunyi haka kuma wani lambar ta bayyana, da sauransu

Abin da yake yi shine nuna maka bazuwar lamba (kowane) tsakanin 0 da 32768 (lamba, ma'ana, ba tare da wakafi ba).

Idan kuna buƙata ta zama bazuwar lamba, amma tsakanin 0 da ... bari mu ce 100, zaku iya sanya wannan iyakar akan sa 😀

amsa kuwwa $ (($ RANDOM% 100))

Hakanan, wani misali ... idan kuna son ya zama lamba tsakanin 0 da 29 zai zama:

amsa kuwwa $ (($ RANDOM% 29))

Shin ba a fahimta ba? 😀

Idan za suyi amfani da shi a cikin rubutun bash da suke yi, don sanya ƙimar da aka samar (lambar bazuwar) zuwa mai canzawa zai zama:

KYAUTA = `` amsa kuwwa $ (($ RANDOM)) ''

Da kyau, wannan shine, ban san game da ku ba ... amma na san cewa zai zama mai amfani a gare ni a wani lokaci hahaha.

gaisuwa


Abubuwan da ke cikin labarin suna bin ka'idodinmu na ka'idojin edita. Don yin rahoton kuskure danna a nan.

9 comments, bar naka

Bar tsokaci

Your email address ba za a buga. Bukata filayen suna alama da *

*

*

  1. Mai alhakin bayanan: Miguel Ángel Gatón
  2. Dalilin bayanan: Gudanar da SPAM, gudanar da sharhi.
  3. Halacci: Yarda da yarda
  4. Sadarwar bayanan: Ba za a sanar da wasu bayanan ga wasu kamfanoni ba sai ta hanyar wajibcin doka.
  5. Ajiye bayanai: Bayanin yanar gizo wanda Occentus Networks (EU) suka dauki nauyi
  6. Hakkoki: A kowane lokaci zaka iyakance, dawo da share bayanan ka.

  1.   Oscar m

    Da kyau, Na gwada shi kuma ya dawo da lambar lambobi 4, yadda za a kara girma?
    Shawara mai ban sha'awa, na gode.

    1.    KZKG ^ Gaara m

      Ya dawo da lamba tsakanin 0 da 32768, ban sami damar samun manyan lambobi ba.

  2.   josue m

    kuma zata iya samarda tsayayyun abubuwa ????

  3.   hankaka m

    KYAUTA = `` amsa kuwwa $ (($ RANDOM)) ''

    Wannan yana aiki amma ba shine mafi kyau ba, saboda sauƙin dalilin cewa RANDOM abu ne mai canzawa kuma zaku iya yin:
    canji = $ RANDOM
    kuma hakane! kar ku sake yin magana a cikin tashar aprte (wanda shine abin da kuke yi)

    1.    KZKG ^ Gaara m

      Ee, a bayyane za a iya cimma hakan kamar haka ... bambancin kawai shi ne daga baya, don ganin lambar da mai canzawar ya dauka (tunda mai amfani ba mai zato ba ne), zai zama dole a yi amsa kuwwa .... kuma a ƙarshe, abin da nake yi anan shine kawai amsa kuwwa (don mai amfani ya iya ganin wane lamba aka ɗauka) daga farko.

      Shin ina fahimtar da kaina? 🙂

  4.   Wuilmer bolivar m

    Wata hanyar don ƙirƙirar lamba bazuwar, kodayake wannan lokacin zai zama wannan umarnin:

    kwanan wata "+% N" | yanke -c 9

    Wannan zai bamu kwanan wata a cikin nanoseconds tare da lambobi 9. Idan muna son adadi guda daya to sai ku sanya "yanke -c 9" (adadi na karshe koyaushe bazuwar ne saboda shine mafi karancin lambar). Idan muna son adadi 2 to sai mu sanya "yanke - c 8,9". Idan muna son siffofi uku to "yanke -c 7-9" (zamu fara amfani da jan layi).

    Abin sani kawai mummunan shine wannan idan muna son samun lambobi da yawa a jere a cikin ɗan gajeren lokaci, saboda wannan bazuwar lamba ce dangane da kwanan wata da lokacin ta. Wannan shine, idan muka yi don tare da wannan umarnin zamu iya ganin cewa:

    $ don ni a 'seq 1 1 500`; yi kwanan wata "+% N"; yi

    ...
    ...
    ...

    308311367
    310807595
    313273093
    315725181
    318186139
    320671403
    323360117
    325733353
    328335462
    330694870
    333259893
    335858999
    338375622
    340798446

    ...
    ...
    ...

    Ina ganin ya bayyana ko? Alƙaluman da ke hannun hagu sun fi kama a cikin ɗan gajeren lokaci, tabbas, kuma waɗanda ke hannun dama sun fi “bazuwar”.

  5.   baki ido m

    mmm… Ina son shi, Ina da karamin rubutu wanda aka zana don samar da lambobin bazuwar, godiya.

  6.   G. m

    Yana hidima .. kuma da yawa ..
    musamman ma idan kuna shirin haɗin keɓaɓɓu a cikin bash tare da kalmar sirri, tsaro, da sauransu, da sauransu, da dai sauransu haha.
    Kyakkyawan aprote.

  7.   José Antonio ya Bi Bent m

    Hello.
    Da farko dai, tabbas, ina taya ku murna da wannan kyakkyawan gidan yanar gizon, wanda na dade ina bibiyar sa.
    Na biyu kuma, yi ɗan ƙaramin bayanin kula ga wannan shigarwar:
    Lokacin iyakancewa ana yin haka:

    amsa kuwwa $ (($ RANDOM% 10))

    A zahiri, abin da kuke umurtar mai fassara shine cewa lambar ku da kuka kirkira koyaushe shine modulus% (saura na rabo) na lambar mai zuwa, a wannan misalin, 10.
    Duk lambar da aka raba ta 10 ba za ta taɓa bayarwa azaman saura abin da ya fi mai rarrabuwa kanta ba.
    Matsalar ita ce ba za ta ba da lambar iri ɗaya ba, saboda rarraba ta 0 ba ma'ana ba ce ga mai fassara.

    Wannan yana nufin cewa amo $ (($ RANDOM% 10) zai ba da sakamako tsakanin 0 da 9, amma ba 10 ba.
    Maganin wannan rikici shine a kara daya zuwa iyakan ku, don haka lamba daya ta fada cikin kewayon bazuwar.

    amsa kuwwa $ (($ RANDOM% 11))

    Wannan zai ba da sakamako tsakanin 0 da 10.

    A gaisuwa.