Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,179 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,376 people online right now. Registration is fast and FREE... Join Now!




Random Number Generating

 
Reply to this topicStart new topic

Random Number Generating, I need values between 0-1 or 1-2

Pationate
1 Jun, 2008 - 05:57 AM
Post #1

New D.I.C Head
*

Joined: 30 May, 2008
Posts: 30


My Contributions
CODE

void __fastcall TForm1::FormCreate(TObject *Sender)
{
double y,ygen;
const double A=35434563456.0 ,B=52453456353.0;
double r;
ygen=A*ygen;
ygen=fmod(ygen,B);
r=ygen/B;
y=ygen;
Label1->Caption=r;
}


Well this algorithm returns me a value like ... 8,31191833346667E-318 can i add some calculation to the random number so that it returns a value between 0-1 , 1-2 .
User is offlineProfile CardPM
+Quote Post

KYA
RE: Random Number Generating
1 Jun, 2008 - 06:03 AM
Post #2

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 5,045



Thanked: 124 times
Dream Kudos: 1200
My Contributions
Seed the rand to time

and thus:

cpp

srand ( time(NULL) );

//this will always give a 1 or 2
//You can change the numbers around to give between 0-2
yourNumber = ((rand() %2)+1);

User is online!Profile CardPM
+Quote Post

Pationate
RE: Random Number Generating
1 Jun, 2008 - 06:14 AM
Post #3

New D.I.C Head
*

Joined: 30 May, 2008
Posts: 30


My Contributions
My tutor ordered me not to use the Rand() function at all times . So i'd prolly have to make a random generating algorithm my self. Eventhough your %2+1 idea is correct. I have to use it in my code , without Rand() .

ps. KYA mate , i really like that you try answering my Questions . Thnx
User is offlineProfile CardPM
+Quote Post

KYA
RE: Random Number Generating
1 Jun, 2008 - 06:17 AM
Post #4

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 5,045



Thanked: 124 times
Dream Kudos: 1200
My Contributions
Man, I hate assignments that make you reinvent the wheel.

If you can use a class, Here is a snippet from this site that can help you out.

This post has been edited by KYA: 1 Jun, 2008 - 06:18 AM
User is online!Profile CardPM
+Quote Post

Pationate
RE: Random Number Generating
1 Jun, 2008 - 06:20 AM
Post #5

New D.I.C Head
*

Joined: 30 May, 2008
Posts: 30


My Contributions
QUOTE(KYA @ 1 Jun, 2008 - 07:17 AM) *

Man, I hate assignments that make you reinvent the wheel.

If you can use a class, Here is a snippet from this site that can help you out.


Agree on that ..
Thnx for the link . I think i'll found out what i need from that .
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Random Number Generating
1 Jun, 2008 - 11:55 AM
Post #6

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,859



Thanked: 50 times
Dream Kudos: 550
My Contributions
There is a nice smile.gif tutorial on random number generators.

Basically you may want to read up on Linear Congruential Generators. Though Voodoo Doll's Mersenne Twister is actually a better algorithm (but harder to understand if you teacher requires you to be able to explain how it works).

LCG's are pretty basic.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 11:04AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month