Advertisement

02.23.2008 at 06:59AM PST, ID: 23187069
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.8

How do you set minimum/maximum values when generating random numbers?

Asked by Visual_Pro in Microsoft Visual C++.Net, C++ Programming Language, Microsoft Visual C#.Net

Tags:

I just have a small question about a console-based random number generating program I've created which uses the CryptGenRandom function.  Basically the random numbers are generating fine but the minimum or maximum number it generates is well into the +/- billions.

Now I know the 2nd property of that function decides the size of the number and originally I was using sizeof(int) but after much effort in experimenting around I can't find a reliable way to make it generate random numbers within a given range that I set (e.g. from 1 to 100).

The best I've been able to do is use this line (full source is below):

if (!CryptGenRandom(Provider, sizeof(char), (unsigned char *) &RetValue))

which by using sizeof(char) is generating random numbers from 0 to 255 for me.  But I'd ideally like to be able to set that as low as 1 to 50.  So if you can give me any advice on what I can do to set a min/max range for the numbers that would be muchly appreciated.  Start Free Trial
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:
#include "stdafx.h"
#include <windows.h>
#include <wincrypt.h>
#include <iostream>
 
#define _WIN32_WINNT 0x0400
 
using namespace std;
 
int __fastcall GenerateRndNum()
   {
   static HCRYPTPROV Provider = NULL;
   int RndNum;
    
   if (!Provider)
    {
    if (!CryptAcquireContext(&Provider, NULL, NULL, PROV_RSA_FULL, 0))
    if (!CryptAcquireContext(&Provider, NULL, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET))
    return(0);
    
    GenerateRndNum(); 
    }
 
   RndNum = 0;
   if (!CryptGenRandom(Provider, sizeof(char), (unsigned char *) &RndNum))
    RndNum = 0;
   return(RndNum);
   }
 
 
int _tmain(int argc, _TCHAR* argv[])
{
	cout << "Calculating..." << endl;
	
         for (i=0;i<50;i++)
	{
		cout << GenerateRndNum() << endl;
	}
 
		cout << "Calculating finished." << endl;
	
	return 0;
}
 
Keywords: How do you set minimum/maximum va…
 
Loading Advertisement...
 
[+][-]02.23.2008 at 07:06AM PST, ID: 20965168

View this solution now by starting your 14-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Microsoft Visual C++.Net, C++ Programming Language, Microsoft Visual C#.Net
Tags: Visual C++, Visual C#
Sign Up Now!
Solution Provided By: evilrix
Participating Experts: 3
Solution Grade: A
 
 
[+][-]02.23.2008 at 08:08AM PST, ID: 20965397

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 14-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.23.2008 at 09:42AM PST, ID: 20965778

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 14-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20081112-EE-VQP-43 / EE_QW_2_20070628