rand is not being rand!!!!!

okay so i have made a function for my ai that i am making for a uni assignment and it what working fine at uni took it home and now it's not being random anymore the sprite(ai) stops at the same spot every time and at uni it was stopping randomly all over the joint and i haven't edited anything!! any idea's why this might be, my only theory is that my screen dimensions are to small for it i am using a 17" and uni screens are all 27" would that make a difference?

anyway here is the code in full if you need any more information let me know =D
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
70
71
72
73
74
75
76
77
78
79

#include <Windows.h>
#include "GL_Functions.h"
#include "pctimer.h"
#include <string>
#include <cmath>
#include <crtdbg.h>
#include <time.h>

int m_iSpritePurpleImage = 0;
int m_iSpritePurpleX = 0;
int m_iSpritePurpleY = 0;

int m_iStrawImage = 0;
int m_iStrawLength = 0;

float m_iSpritePurpleSpeedX = 1.0f;
float m_iSpritePurpleSpeedY = 0.0f;

float m_iRandomNumber = 0;

int main(int argc, char* argv[])
{
	//Lets open the window and initialise opengl
	InitGL(1024,768);
	
	int m_iSpritePurpleImage = LoadTexture("./Images/SpritePurple.png");
	int m_iStrawImage = LoadTexture("./Images/Straw.png");
	m_iSpritePurpleX = 0;
	m_iSpritePurpleY = 200;
	srand(unsigned int(GetTimeElapsed));
	m_iRandomNumber = rand() % ((1024/8)*6) + 1024/8;
	do
	{
		//Clear the screen, so previous frames don't build up
		ClearScreen();

	if (m_iSpritePurpleX >= m_iRandomNumber )
	{
		 m_iSpritePurpleSpeedX = 0;
		 m_iStrawLength += 1;
		 if (m_iStrawLength >= 400)
		 {
			 m_iStrawLength = 400;
		 }
		 SetTintColour(0.2,0.7,0);
		 DrawSprite(m_iStrawImage,m_iSpritePurpleX + 17 ,m_iSpritePurpleY,5,m_iStrawLength);
		 SetTintColour(1,1,1);
	}
	else if(m_iSpritePurpleX <= 0 || m_iSpritePurpleX >= 1024)
	{
		m_iSpritePurpleX = 50;
		m_iSpritePurpleY = 200;
	}
	
	m_iSpritePurpleX += m_iSpritePurpleSpeedX;
	m_iSpritePurpleY += m_iSpritePurpleSpeedY;
	
	DrawSprite(m_iSpritePurpleImage,m_iSpritePurpleX,m_iSpritePurpleY,34,34);
	
	//Stop it from running too fast! Sleep ZZzzz
	Sleep(5);
		
	} while (FrameworkUpdate()); //Do some secret stuff, 

	//Before you exit, clean up after yourself
	FreeTexture( m_iSpritePurpleImage);
	FreeTexture( m_iStrawImage);
	//Close down
	CloseDown();

	_CrtDumpMemoryLeaks();

	//Quit!
	return 0;
}


srand(clock())
set random number by clock()
it will stop being random because time elapsed will be consistent based on the processing speed of the computer.
Last edited on
so how would i go about changing that.....?

do
srand(clock())

rand(NULL)
or
rand()

that is about as random you can get without making a random generator yourself.
Use rand_s() or directly call SystemFunction036 exported from advapi32.dll
This is guaranteed by the operating system to return a unique number. No need to call srand first if you use this.
what syntac/code do i write to use that systemfunction?
or directly call SystemFunction036 exported from advapi32.dll


waaaaaat?

Why would you recommend he do something so incredibly obscure?


programmeraie: don't listen to these guys. using rand is very simple:

1
2
3
4
5
// do this once at program startup (like at the start of main())
srand( (unsigned)time(0) );

// do this whenever you want a random number:
int r = rand();
here is an old and simple algorithm from hal chamberlin for unsigned long ints:

nrnd = 196314165 * nrnd + 907633515;

this is suitable for audio rate white noise, so it ought to work quite well for events in a game.
Topic archived. No new replies allowed.