How to Make a Random number (int) that changes every start?

Hey guys

So im making a console simulator.

Its basicly a game about universe and stuff.


So i need to make a random int for some voids and i want to make a exact random int that changes every game start!

The basic Rand() dont work because the first number is everytime 41!

How can i make a system that will change every game start?
try this one:

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
static UINT uiRealRand;  //in BSS

void MySRand(void)
{
	__asm
	{
		push	edx
		rdtsc  //in edx:eax
		mov		uiRealRand,eax
		pop		edx
	}
	srand(uiRealRand);
}

UINT MyRand(void)
{
	int iLibRand = rand();  //only 15bit

	__asm
	{
		push	ecx
		mov		ecx,iLibRand
		and		ecx,0xFF
		or		cl,1  //loop overflow
		mov		eax,uiRealRand  //full 32bit

m1_:	rcl		eax,cl
		add		eax,uiRealRand
		loop	m1_

		mov		ecx,iLibRand
		and		ecx,0xFF00
		xchg	ch,cl
		or		cl,1

m2_:	rcl		eax,cl
		add		eax,uiRealRand
		loop	m2_

		mov		uiRealRand,eax
		pop		ecx
	}
	return	uiRealRand;
}

UINT MyGetRandRange(UINT Min, UINT Max)
{
	return ( (MyRand() % ((Max + 1)- Min) ) + Min);
//	return ( (rand() % ((Max + 1)- Min) ) + Min); // stdlib
}
Last edited on
can i make a header like: rand.h
then include it to main.cpp and then use it?

another question: What i need to change there uin your code? if nothing what i need to declare for use the actual number=?
Excuse me, but isn't inline assembly overdoing it here? All one needs to do is use std::srand(std::time(0)); (after #including <ctime>) for the purposes of making rand()'s values change every run of the program. :/

@vitinho444:
Absolutely you can make your own header files. If they're in the same directory as your .cpp files, all you need to do to include them is use #include "name" .

-Albatross
Last edited on
include this in your personal stuff file (mystuff.cpp).

than in main:

1
2
3
MySRand();

int iValue = MyGetRandRange(1, 9);


is all you have to do...
thanks so i just need to make : (using dev cpp xD)
srand(time(0));?
then for use the "number" what i write?


@dutChBZ

but man. if i wanna put:

if (RANDNAME > 100)
{

HI}

if (RANDNAME < 100)
{
BYE
}

what is the name i give to it?
its that part that i missed
Last edited on
this is still C-code... in C++ you create an object and let the ctor make the init stuff :-)
To get a number from rand() within a certain range, that's a different trial. One of the simplest ways is to use
std::rand() % upper + offset;

...which will return random numbers in the range of [offset,upper+offset-1].

The probability of getting lower numbers is somewhat higher than getting larger numbers for this method, but for most purposes it works just fine. :)

-Albatross
 
MyGetRandRange(0, 1) ? "Hi" : "Bye";
@Albatross
i used your and it uses everytime the same number..

@dutChBZ

are u saying that the name to call the function is MyGetRandRange?

if yes can i change it to random?
if yes can i change it to random?


Just do it. (Where is your 'Forscherdrang')
@vitinho444: If you used the srand snippet I gave you, it shouldn't. :)

1
2
3
4
5
6
7
8
9
10
//...
#include <cstdlib>
#include <ctime>
//...

//...
int main()
{
    std::srand(std::time(0)); //Makes rand() random. :)
//... 


http://www.cplusplus.com/reference/clibrary/cstdlib/srand/

-Albatross
Last edited on
@Albatross

ok i figured out thanks all!

now it worked
Last edited on
Topic archived. No new replies allowed.