Random in Cgl?

Hello,

im trying to make a simple cgl game. but i just cant find how to give a random number to a int in CGL can someone help me out?

i want to give the direction a random number from 0 to 1 whats the best way to do this?


heres my 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
#include <stdio.h>
#include <math.h>
#include "c_enemy.h"


void c_enemy::Init()

{
	direction=0;
	x=450;
	y=0;
}

void c_enemy::Draw()

{
	    if (direction==1) CGL_DrawSprite(x-scrollx,y-scrolly,spr_enemy);
    	else CGL_DrawMirroredSprite(x-scrollx,y-scrolly,spr_enemy);
}

void c_enemy::Handle()

{
	int colorfeet,colorhead,colorright,colorleft,colorright2,colorleft2;   
	
	 // Check Collision met het bitmap masker
	colorfeet=CGL_GetPixel(x+32,y+62,bmp_mask); 
	colorright=CGL_GetPixel(x+64,y+32,bmp_mask);
	colorleft=CGL_GetPixel(x,y+32,bmp_mask);	
	
	if (colorleft==COLOR_FLOOR) direction=1;
		
	if (colorright==COLOR_FLOOR) direction=0;
	     
	if (colorfeet==COLOR_FLOOR) gravity=0.0;
	else{
		gravity+=0.1;
	}
	
	if (direction==1){
		x++;
	}
	else{
		x--;
	}
	
	y+=gravity;
	CGL_DrawText(10,120,gamefont,"%d",direction);
}
i forgot something to ask:

i need to make more enemys moving at the same time whats the best way i can do this?

thanks!
> i want to give the direction a random number from 0 to 1 whats the best way to do this?

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

int main()
{
    // http://en.cppreference.com/w/cpp/numeric/random/mersenne_twister_engine
    std::mt19937 rng( std::time(nullptr) ) ;

    // generate and print 100 random numbers uniformly distributed in [0,1)
    // http://en.cppreference.com/w/cpp/numeric/random/uniform_real_distribution
    std::uniform_real_distribution<double> distribution( 0, 1 ) ;
    for( int i = 0 ; i < 10 ; ++i )
    {
        for( int j = 0 ; j < 10 ; ++j )
            std::cout << std::fixed << std::setw(5) << distribution(rng) << ' ' ;
        std::cout << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/9c2294ae453ee5d6
sorry but i dont really get how thats working :/ is there a more simple way?
> is there a more simple way?

Difficult to find something simpler than those few lines of code.

Perhaps you are looking for something more familiar; something like this:

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

int main()
{
    // http://en.cppreference.com/w/cpp/numeric/random/srand
    std::srand( std::time(nullptr) ) ;

    // generate and print 100 random numbers aaproximately uniformly distributed in [0,1]
    // http://en.cppreference.com/w/cpp/numeric/random/rand
    for( int i = 0 ; i < 10 ; ++i )
    {
        for( int j = 0 ; j < 10 ; ++j )
            std::cout << std::fixed << std::rand() / double(RAND_MAX) << ' ' ;
        std::cout << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/b14a791157689169
Topic archived. No new replies allowed.