• Najnowsze pytania
  • Bez odpowiedzi
  • Zadaj pytanie
  • Kategorie
  • Tagi
  • Zdobyte punkty
  • Ekipa ninja
  • IRC
  • FAQ
  • Regulamin
  • Książki warte uwagi

Odgadywanie wylosowanych liczb - program

Object Storage Arubacloud
0 głosów
412 wizyt
pytanie zadane 12 września 2016 w C i C++ przez niezalogowany
Witam, piszę program którego zadaniem będzie odgadywanie wylosowanych liczb.

Tu nasuwa się problem, nie wiem gdzie znajduje się ciało funkcji Rand(), jest to biblioteka <cstdlib> lecz nie mogę nigdzie jej znaleźć :(

Poszukuję także funkcji math.random() LUA :)

Chciałbym zobaczyć jak one są zbudowane :)

 

Pozdrawiam :)
komentarz 12 września 2016 przez DragonCoder Nałogowiec (36,500 p.)
Inkludujesz cstdlib i dajesz na poczatku programu srand (time (NULL)); i pozniej np. Wynik=rand ()%50+1;
komentarz 12 września 2016 przez niezalogowany
Tak, ale ja potrzebuje ciało funkcji Rand(), to jak ona jest zbudowana. Wiem jak ją użyć :)

Czyli np.

int Rand(){

       // jakieś obliczenia

return obliczenia;

}

2 odpowiedzi

0 głosów
odpowiedź 12 września 2016 przez unknown Nałogowiec (39,560 p.)
edycja 12 września 2016 przez unknown

Tu nasuwa się problem, nie wiem gdzie znajduje się ciało funkcji Rand()

Skoro piszesz program

którego zadaniem będzie odgadywanie wylosowanych liczb.

to nie rozumiem po co Ci to. Ale skoro chcesz.

Ciało funkcji rand będzie się różnić zależnie od implementacji. Jednak większość implementacji i tak korzysta z LCG( https://en.wikipedia.org/wiki/Linear_congruential_generator ). Implementacja funkcji rand z glibc 2.24:

int
rand (void)
{
  return (int) __random ();
}
long int
__random (void)
{
  int32_t retval;

  __libc_lock_lock (lock);

  (void) __random_r (&unsafe_state, &retval);

  __libc_lock_unlock (lock);

  return retval;
}
int
__random_r (struct random_data *buf, int32_t *result)
{
  int32_t *state;

  if (buf == NULL || result == NULL)
    goto fail;

  state = buf->state;

  if (buf->rand_type == TYPE_0)
    {
      int32_t val = state[0];
      val = ((state[0] * 1103515245) + 12345) & 0x7fffffff;
      state[0] = val;
      *result = val;
    }
  else
    {
      int32_t *fptr = buf->fptr;
      int32_t *rptr = buf->rptr;
      int32_t *end_ptr = buf->end_ptr;
      int32_t val;

      val = *fptr += *rptr;
      /* Chucking least random bit.  */
      *result = (val >> 1) & 0x7fffffff;
      ++fptr;
      if (fptr >= end_ptr)
	{
	  fptr = state;
	  ++rptr;
	}
      else
	{
	  ++rptr;
	  if (rptr >= end_ptr)
	    rptr = state;
	}
      buf->fptr = fptr;
      buf->rptr = rptr;
    }
  return 0;

 fail:
  __set_errno (EINVAL);
  return -1;
}
#define	TYPE_0		0
#define	BREAK_0		8
#define	DEG_0		0
#define	SEP_0		0

/* x**7 + x**3 + 1.  */
#define	TYPE_1		1
#define	BREAK_1		32
#define	DEG_1		7
#define	SEP_1		3

/* x**15 + x + 1.  */
#define	TYPE_2		2
#define	BREAK_2		64
#define	DEG_2		15
#define	SEP_2		1

/* x**31 + x**3 + 1.  */
#define	TYPE_3		3
#define	BREAK_3		128
#define	DEG_3		31
#define	SEP_3		3

/* x**63 + x + 1.  */
#define	TYPE_4		4
#define	BREAK_4		256
#define	DEG_4		63
#define	SEP_4		1


/* Array versions of the above information to make code run faster.
   Relies on fact that TYPE_i == i.  */

#define	MAX_TYPES	5	/* Max number of types above.  */


/* Initially, everything is set up as if from:
	initstate(1, randtbl, 128);
   Note that this initialization takes advantage of the fact that srandom
   advances the front and rear pointers 10*rand_deg times, and hence the
   rear pointer which starts at 0 will also end up at zero; thus the zeroth
   element of the state information, which contains info about the current
   position of the rear pointer is just
	(MAX_TYPES * (rptr - state)) + TYPE_3 == TYPE_3.  */

static int32_t randtbl[DEG_3 + 1] =
  {
    TYPE_3,

    -1726662223, 379960547, 1735697613, 1040273694, 1313901226,
    1627687941, -179304937, -2073333483, 1780058412, -1989503057,
    -615974602, 344556628, 939512070, -1249116260, 1507946756,
    -812545463, 154635395, 1388815473, -1926676823, 525320961,
    -1009028674, 968117788, -123449607, 1284210865, 435012392,
    -2017506339, -911064859, -370259173, 1132637927, 1398500161,
    -205601318,
  };


static struct random_data unsafe_state =
  {
/* FPTR and RPTR are two pointers into the state info, a front and a rear
   pointer.  These two pointers are always rand_sep places apart, as they
   cycle through the state information.  (Yes, this does mean we could get
   away with just one pointer, but the code for random is more efficient
   this way).  The pointers are left positioned as they would be from the call:
	initstate(1, randtbl, 128);
   (The position of the rear pointer, rptr, is really 0 (as explained above
   in the initialization of randtbl) because the state table pointer is set
   to point to randtbl[1] (as explained below).)  */

    .fptr = &randtbl[SEP_3 + 1],
    .rptr = &randtbl[1],

/* The following things are the pointer to the state information table,
   the type of the current generator, the degree of the current polynomial
   being used, and the separation between the two pointers.
   Note that for efficiency of random, we remember the first location of
   the state information, not the zeroth.  Hence it is valid to access
   state[-1], which is used to store the type of the R.N.G.
   Also, we remember the last location, since this is more efficient than
   indexing every time to find the address of the last element to see if
   the front and rear pointers have wrapped.  */

    .state = &randtbl[1],

    .rand_type = TYPE_3,
    .rand_deg = DEG_3,
    .rand_sep = SEP_3,

    .end_ptr = &randtbl[sizeof (randtbl) / sizeof (randtbl[0])]
};

btw. W C++ zamiast rand powinno się korzystać z biblioteki random, której algorytmy losowania liczb są o wiele lepsze.

edit: standard C zawiera bardzo prostą implementacje funkcji rand:

static unsigned long int next = 1;

int rand(void) // RAND_MAX assumed to be 32767
{
    next = next * 1103515245 + 12345;
    return (unsigned int)(next/65536) % 32768;
}

void srand(unsigned int seed)
{
    next = seed;
}

 

komentarz 12 września 2016 przez niezalogowany
Potrzebuje to, gdyż chcę przewidzieć ruch kostki w grze. :) Pozdrawiam
0 głosów
odpowiedź 12 września 2016 przez MichuDev Pasjonat (20,300 p.)

Pliki nagłówkowe pisane w ten sposób <nazwa> trzyma kompilator w swoich plikach, w przypadku GCC jest to katalog include. Jednak nie zawsze możesz mieć dostęp do tej funkcji.

  • C i C++ są językami, które nie wymagają dużo, by móc je skompilować pod daną architekturę, system itp. Co daje wielkie pole do popisu. Standard mówi jak coś się ma zachowywać, ale nic nie mówi o wynikowym kodzie maszynowym.
  • Kompilator może wiele optymalizować, może mieć dostępny kod źródłowy, podobnie z systemami.
  • W systemie Windows wszystkie funkcje języków C/C++ są w msvcrt.dll (C:\Windows\System32), w przypadku innych systemów jest inaczej, ale jest to też w bibliotekach dynamicznych.
  • Warto zobaczyć ten film, jak cię to ciekawi: 

https://www.youtube.com/watch?v=u43y_WE52Ig

Gdybyś miał jeszcze pytania to pisz!

komentarz 12 września 2016 przez niezalogowany
Dziękuję, za tak liczne komentarze. Przeanalizuje wszystko dogłębnie.

Zależy mi jeszcze na definicji funkcji math.random()  implementacja w LUA.

Podobne pytania

0 głosów
0 odpowiedzi 710 wizyt
pytanie zadane 14 maja 2020 w C i C++ przez lujasjeden Użytkownik (860 p.)
0 głosów
1 odpowiedź 3,518 wizyt

92,552 zapytań

141,400 odpowiedzi

319,532 komentarzy

61,938 pasjonatów

Motyw:

Akcja Pajacyk

Pajacyk od wielu lat dożywia dzieci. Pomóż klikając w zielony brzuszek na stronie. Dziękujemy! ♡

Oto polecana książka warta uwagi.
Pełną listę książek znajdziesz tutaj.

Akademia Sekuraka

Kolejna edycja największej imprezy hakerskiej w Polsce, czyli Mega Sekurak Hacking Party odbędzie się już 20 maja 2024r. Z tej okazji mamy dla Was kod: pasjamshp - jeżeli wpiszecie go w koszyku, to wówczas otrzymacie 40% zniżki na bilet w wersji standard!

Więcej informacji na temat imprezy znajdziecie tutaj. Dziękujemy ekipie Sekuraka za taką fajną zniżkę dla wszystkich Pasjonatów!

Akademia Sekuraka

Niedawno wystartował dodruk tej świetnej, rozchwytywanej książki (około 940 stron). Mamy dla Was kod: pasja (wpiszcie go w koszyku), dzięki któremu otrzymujemy 10% zniżki - dziękujemy zaprzyjaźnionej ekipie Sekuraka za taki bonus dla Pasjonatów! Książka to pierwszy tom z serii o ITsec, który łagodnie wprowadzi w świat bezpieczeństwa IT każdą osobę - warto, polecamy!

...