Anytime Input?

Pages: 1234
closed account (4Gb4jE8b)
what keeps the number from being really large is the modulo. What that particular operator does is return the remainder of the random number/the other number. If you remember 5th grade math (which i couldn't blame you if you don't), the remainder has to be less than the divisor so, with rand() %3, the modulo has to be less than 3.

My bad for forgetting to tell you to seed it, they didn't really say it on the page i gave you for it either :P

+1 to wolfgangs code, i like it

and yes to your post that was nearly the same time as mine, those are all correct because possible remainders for a number divided by 3 is 0,1,2 for five 0,1,2,3,4 so on so forth
Last edited on
i just noticed in wolfgangs code if you change time to time_t you eliminate the warning message. just nitpicking.

 
srand(time_t(0));
Last edited on
As to it should be a 1/3 or 1/5 or 1/11 than answer is yes. More proof via edited code. Just change the top and the mod vars to see the change you'd get.
It may be off as this is chance like flipping a coin. We expect 50-50, but we only rarely get 50-50.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <ctime> // for time()
#include <cstdlib> // for rand() and srand()
#include <iomanip> // setprecision
#include <iostream>

using namespace std;

int main()
{
  srand( time (0) ); // SEED!
  const int mod = 3;
  const int top = 1;

  cout << "MAXIMUM RANDOM NUMBER VIA rand(): " << RAND_MAX << endl << endl;

  int var, count = 0;
  double chance;

  const int runs = 100000;
  for (int i = 0; i < runs; ++i)
  {
    var = rand();
    if ( (var % mod) < top )
      ++count;
  }

  chance = double(count) / double(runs);
  cout << "Percent chance of getting < " << top << ": " << fixed << setprecision(3)
       << chance << "%" << endl << endl;
  cout << "This should be close to a " << top << "/" << mod << " chance!" << endl;

  return 0;
}
Last edited on
hmm, okay reassurring myself, this is how i'm grasping this

if(rand() % (number that would usually be on bottom of a fraction) < (Number that'd be on top on a fraction) )
{
//do if true
}
else
//do if false :D
And that is chance yes. So 2/3 would be ( (rand() % 3) < 2) and that gives you a 2/3 chance which means you're more likely to call the if statement.
closed account (4Gb4jE8b)
perfect randomness is impossible I thought. But we must remember, this is only for a highschool game. Pseudorandomness is allowed :P
Perfect random in the world of computers is a pseudodream.
acron.. don't seed with time_t(0) that is a cast and every run of the function will produce the same sequence. We seed with time(0) to try to ensure that there will be a different starting value each time... you can cast that to unsigned or whatever the warning is for, but don't just seed with 0...
ok thanks for the info. i should have checked it.
Thank you all, haha and yes, This was only for a highschool game, but think of it this way. In the near future, if I become a game programmer, i'll know and understand this stuff because of you guyd :D and im now in love with this site because its such an active forum, compared to other where you have to wait a day or two for a response. and for now, my game is going to be simple, with extra random things to make it interesting. I'll post a link of the current code and how different it is from this morning. (Be warned, being in console i added a cheesy effect :D)
closed account (4Gb4jE8b)
I admit, today was a particularly fast day for your thread :P don't always expect that (like for homework *cough cough*) but i check on a daily basis and if i think i can answer a problem from the topic title, then i check it out at least :P
i followed this thread, but my result is not random. it goes gradually random from low to high then back to low again. it doesnt jump back and forth from high to low like it should. they just get progressively higher until its close to 100 then starts off low again.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <cstdio>

using namespace std;

int main(int argc, char * argv[])
{
	srand(time(0));
	int result;
	result = rand() % 100 + 1;

	cout << ("First number: %d\n", result);


	getchar();
}
Last edited on
Its pseudo random numbers. It may not always be perfect. I admit that I actually stopped using rand() from <cstdlib> a while ago and switched to using the boost random library. Its a bit complex to set up but I just added the code I use a snippet and place it into my programs now.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <boost/random/uniform_int.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/variate_generator.hpp>

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <cstdio>

using namespace std;

// Global Gen Var
boost::mt19937 gen;

int rand(int min, int max) // rand function using boost lib
{
    boost::uniform_int<> dist(min, max);
    boost::variate_generator< boost::mt19937&, boost::uniform_int<> > rnd(gen, dist);
    return rnd();
}

int main(int argc, char * argv[])
{
	srand((unsigned)time(0)); // Seed
	gen.seed((unsigned)std::time(0)); // Seed

	int result;

	for(int i = 0; i < 25; ++i)
	{
           result = rand() % 100 + 1;
           cout << "Number using cstdlib : " << result << endl;
	}

	for(int i = 0; i < 25; ++i)
          cout << "Number using boostlib : " << rand(1, 100) << endl;

	getchar();
}
Number using cstdlib : 43
Number using cstdlib : 11
Number using cstdlib : 20
Number using cstdlib : 53
Number using cstdlib : 77
Number using cstdlib : 17
Number using cstdlib : 7
Number using cstdlib : 50
Number using cstdlib : 55
Number using cstdlib : 85
Number using cstdlib : 43
Number using cstdlib : 39
Number using cstdlib : 87
Number using cstdlib : 63
Number using cstdlib : 36
Number using cstdlib : 84
Number using cstdlib : 25
Number using cstdlib : 84
Number using cstdlib : 21
Number using cstdlib : 94
Number using cstdlib : 55
Number using cstdlib : 41
Number using cstdlib : 7
Number using cstdlib : 92
Number using cstdlib : 86
Number using boostlib : 20
Number using boostlib : 52
Number using boostlib : 30
Number using boostlib : 60
Number using boostlib : 16
Number using boostlib : 98
Number using boostlib : 76
Number using boostlib : 85
Number using boostlib : 14
Number using boostlib : 67
Number using boostlib : 39
Number using boostlib : 91
Number using boostlib : 52
Number using boostlib : 33
Number using boostlib : 20
Number using boostlib : 27
Number using boostlib : 23
Number using boostlib : 42
Number using boostlib : 25
Number using boostlib : 36
Number using boostlib : 69
Number using boostlib : 96
Number using boostlib : 85
Number using boostlib : 40
Number using boostlib : 69


Process returned 0 (0x0)   execution time : 19.027 s
Press any key to continue.
http://pastebin.com/36saUC6G
for anyone who would like to test it :D
thanks for the help guys, but i'm getting off for tonight , thanks again everyone
Last edited on
closed account (4Gb4jE8b)
i shall tomorrow and get back to you, i too am off for tonight.
have a good night cstorm.

thanks wolfgang that looks alot more random.
closed account (4Gb4jE8b)
indeed it does, (and i lied, i'm excited about programming, so i'm gonna program some more), but wolfgang, would you mind setting up some stats for us from it?

(i would but i'm programming an epic "..." effect ^.^)
Stats? What stats would you like?
closed account (4Gb4jE8b)
oh just like you had here

cout << "Percent chance of getting < " << top << ": " << fixed << setprecision(3)
<< chance << "%" << endl << endl;
cout << "This should be close to a " << top << "/" << mod << " chance!" << endl;
Statistics: (Slightly ugly code!)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <boost/random/uniform_int.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/variate_generator.hpp>

#include <ctime>
#include <limits>
#include <string>
#include <iomanip>
#include <cstdlib>
#include <iostream>

// Global Gen Var
boost::mt19937 gen;

// Prototypes
int rand(int, int);
void random_statistics(std::string, unsigned long long, std::string, int, int, bool);

// BEGIN
int main(int argc, char * argv[])
{
	srand   ((unsigned)std::time(0)); // Seed
	gen.seed((unsigned)std::time(0)); // Seed

  const int mod = 3;
  const int top = 1;

  // CSTDLIB
  random_statistics("std::rand()", RAND_MAX, "Using % Opperator", top, mod, false);
  // BOOST LIB 1
  random_statistics("boost::rand():", RAND_MAX, "Limiting to CSTDLIB MAX, USING % METHOD", top, mod, true);
  // BOOST LIB 2
  random_statistics("boost::rand():", std::numeric_limits<int>::max(), "Limiting to MAX INT, USING % METHOD", top, mod, true);
  // BOOST LIB 3
  random_statistics("boost::rand():", mod-1, "LIMITING TO 3, Directly randomizing [0, 3)", top, mod, true);

  return 0;
}

int rand(int min, int max)
{
    boost::uniform_int<> dist(min, max);
    boost::variate_generator< boost::mt19937&, boost::uniform_int<> > rnd(gen, dist);
    return rnd();
}

void random_statistics(std::string mtd, unsigned long long max, std::string useage, int top, int mod, bool ver)
{
  std::cout << "// ===================================================== //" << std::endl;
  std::cout << "MAXIMUM RANDOM NUMBER VIA " << mtd << " " << max << std::endl;
  std::cout << useage << std::endl << std::endl;

  int var, count = 0;
  double chance;
  const int runs = 100000;

  for (int i = 0; i < runs; ++i)
  {
    (ver) ? ( var = rand(0, max) ) : ( var = std::rand() );
    if ( (var % mod) < top )
      ++count;
  }


  chance = double(count) / double(runs);
  std::cout << "Percent chance of getting < " << top << ": " << std::fixed << std::setprecision(3)
       << chance << "%" << std::endl;
  std::cout << "This should be close to a " << top << "/" << mod << " chance!" << std::endl << std::endl;
}
// ===================================================== //
MAXIMUM RANDOM NUMBER VIA std::rand() 32767
Using % Opperator

Percent chance of getting < 1: 0.332%
This should be close to a 1/3 chance!

// ===================================================== //
MAXIMUM RANDOM NUMBER VIA boost::rand(): 32767
Limiting to CSTDLIB MAX, USING % METHOD

Percent chance of getting < 1: 0.333%
This should be close to a 1/3 chance!

// ===================================================== //
MAXIMUM RANDOM NUMBER VIA boost::rand(): 2147483647
Limiting to MAX INT, USING % METHOD

Percent chance of getting < 1: 0.333%
This should be close to a 1/3 chance!

// ===================================================== //
MAXIMUM RANDOM NUMBER VIA boost::rand(): 2
LIMITING TO 3, Directly randomizing [0, 3)

Percent chance of getting < 1: 0.333%
This should be close to a 1/3 chance!


Process returned 0 (0x0)   execution time : 0.156 s
Press any key to continue.

// RUN 2
// ===================================================== //
MAXIMUM RANDOM NUMBER VIA std::rand() 32767
Using % Opperator

Percent chance of getting < 1: 0.335%
This should be close to a 1/3 chance!

// ===================================================== //
MAXIMUM RANDOM NUMBER VIA boost::rand(): 32767
Limiting to CSTDLIB MAX, USING % METHOD

Percent chance of getting < 1: 0.333%
This should be close to a 1/3 chance!

// ===================================================== //
MAXIMUM RANDOM NUMBER VIA boost::rand(): 2147483647
Limiting to MAX INT, USING % METHOD

Percent chance of getting < 1: 0.334%
This should be close to a 1/3 chance!

// ===================================================== //
MAXIMUM RANDOM NUMBER VIA boost::rand(): 2
LIMITING TO 3, Directly randomizing [0, 3)

Percent chance of getting < 1: 0.335%
This should be close to a 1/3 chance!


Process returned 0 (0x0)   execution time : 0.161 s
Press any key to continue.
Pages: 1234