8 Replies - 10064 Views - Last Post: 06 September 2007 - 10:22 AM Rate Topic: -----

#1 kapoorhimanshu_1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 05-September 07

Random no generator without using windows API in c++

Post icon  Posted 05 September 2007 - 03:48 AM

Hi All,

I want to generate random no's ,in C++ , without using inbulit function like rand() and all. So i'll be really greatfull , if some one can tell me how to proceed further , algorithms for random no. generation except Mersenne Twister algorith , will really b a great help.

Thnx
Himanshu Kapoor
Is This A Good Question/Topic? 0
  • +

Replies To: Random no generator without using windows API in c++

#2 Pontus  Icon User is offline

  • Cattlebruiser

Reputation: 18
  • View blog
  • Posts: 612
  • Joined: 28-December 06

Re: Random no generator without using windows API in c++

Posted 05 September 2007 - 04:04 AM

if by no u mean a number then u can use this
EDIT: just noticed in your previous post u dont want rand(), gonna keep the code for other ppl though, and whats wrong with the rand() function?
#include <iostream>
#inclide <cstdlib> //for the random number function
#include <ctime> //for the seeding value

using namespace std;

int main()
{ 
 int randnumber;
 srand(time(0));// seed your random value
 randnumber=rand() %100 +50; //generates a number between 50 and 100
 cout<<"Your random number is "<<randnumber<<endl;
 cin.get();
 return 0;
}


This post has been edited by manhaeve5: 05 September 2007 - 04:07 AM

Was This Post Helpful? 0
  • +
  • -

#3 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 856
  • View blog
  • Posts: 2,339
  • Joined: 20-August 07

Re: Random no generator without using windows API in c++

Posted 05 September 2007 - 05:07 AM

This sounds to me more like an algorithm question than a C++ question. Although IMHO, rand() is generally random enough for most things (Maybe not for a slot machine at a casino, but for the average program its good enough).

If you're interested in a better way of using rand (Mainly, a better way of seeding with srand() ) then have a look here
http://www.eternally...w_art_rand.aspx

Otherwise, you could try looking around in algorithm forums, or do a google search for algorithms. C and C++ do not have any other random number facilities.. (Yet!)
Was This Post Helpful? 0
  • +
  • -

#4 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Random no generator without using windows API in c++

Posted 05 September 2007 - 05:33 AM

View Postmanhaeve5, on 5 Sep, 2007 - 07:04 AM, said:

if by no u mean a number then u can use this
EDIT: just noticed in your previous post u dont want rand(), gonna keep the code for other ppl though, and whats wrong with the rand() function?

since rand() is not actually random, many programmers implement other forms of random number generators.
Was This Post Helpful? 0
  • +
  • -

#5 Xing  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 19
  • View blog
  • Posts: 725
  • Joined: 22-July 06

Re: Random no generator without using windows API in c++

Posted 05 September 2007 - 06:05 AM

The Boost Random Number Library (Boost.Random for short) provides a vast variety of generators and distributions to produce random numbers having useful properties, such as
a ) Uniform Random Number Generator
b ) Non-deterministic Uniform Random Number Generator
c ) Pseudo-Random Number Generator
d ) Random Distribution
e ) Quasi-Random Number Generators etc.

This post has been edited by Xing: 05 September 2007 - 06:06 AM

Was This Post Helpful? 0
  • +
  • -

#6 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Random no generator without using windows API in c++

Posted 05 September 2007 - 12:59 PM

rand() is not a part of the Windows API.
I have a feeling that your assignment question could be: "Generate A Random Number without using rand(), by using the Windows API"

If that is so, use the GetTickCount() API function and use the result as the seed for any of the algorithms that are mentioned above.
Was This Post Helpful? 0
  • +
  • -

#7 Pontus  Icon User is offline

  • Cattlebruiser

Reputation: 18
  • View blog
  • Posts: 612
  • Joined: 28-December 06

Re: Random no generator without using windows API in c++

Posted 05 September 2007 - 01:54 PM

Let me remind u that a computer with no input from natural things, like noise, heat, light. Cant make true random numbers, all of there are pseude-random. The only gamingsystem was the snes i believe, taking the noise made by a fan.
Was This Post Helpful? 0
  • +
  • -

#8 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Random no generator without using windows API in c++

Posted 05 September 2007 - 02:08 PM

But you can make a better random number generator than rand(). I believe that was the point.
Was This Post Helpful? 0
  • +
  • -

#9 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Random no generator without using windows API in c++

Posted 06 September 2007 - 10:22 AM

Quote

The only gamingsystem was the snes i believe, taking the noise made by a fan

Wow, I didn't know that.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1